Saturday, 2 November 2013

How to create windows installer msi file in python



We can create window installer msi file from different ways but I prefer cx_freeze library

In previous post “how to create exefile by py2exe”, we have made exe file with library.

Now we will create window installer msi file, msi file is a installer file for window. It will install this application in your system and create desktop shortcut. So when you will double click on icon it will open your sample.exe window

For window installer file you need some file in 1 folder

  • keep exe file only in one folder
  • create setup.py for msi file which is given below
  • keep all image, files which is required for exe
  • install cx_freeze library
Cx_freeze Windows installers (MSI):


setup.py
from cx_Freeze import *

includefiles=["win32api.pyd","smaple.exe,"favicon.ico","pythoncom27.dll","pywintypes27.dll”,"images/cream_dust.png "]
excludes=[]
packages=[]
base = None
if sys.platform == "win32":
    base = "Win32GUI"
    
shortcut_table = [
    ("DesktopShortcut",        # Shortcut
     "DesktopFolder",          # Directory_
     "Tool",           # Name
     "TARGETDIR",              # Component_
     "[TARGETDIR]\sample.exe",# Target
     None,                     # Arguments
     None,                     # Description
     None,                     # Hotkey
     None,                     # Icon
     None,                     # IconIndex
     None,                     # ShowCmd
     "TARGETDIR",               # WkDir
     )
    ]

# Now create the table dictionary
msi_data = {"Shortcut": shortcut_table}

# Change some default MSI options and specify the use of the above defined tables
bdist_msi_options = {'data': msi_data}
setup(
     version = "0.1",
     description = "Demo Tool",
     author = "Ashish",
     name = "Demo Tool",
     options = {'build_exe': {'include_files':includefiles}, "bdist_msi": bdist_msi_options,},
     executables = [
        Executable(
            script="sample.py",
            base=base,
            icon='favicon.ico',
            )
        ]
     )



Shortcut table is for desktop shortcut icon and startup menu.

After creating setup.py then open cmd prompt and go to your directory.  Now type

        Python setup.py py2exe

Then enter, it will create msi file in dist folder , now you can install this msi file in your system and click on desktop icon it will open your app sample window.
 
Thanks guys

3 comments:

  1. import cx_Freeze
    import sys

    base = None

    if sys.platform == 'Win32':
    base = "Win32GUI"

    executables = [cx_Freeze.Executable("Perimeter App.py",base="Win32GUI",shortcutName="MathApp", shortcutDir="G:\folders from C drive\school\foundation\untitled", icon="math.ico")]

    cx_Freeze.setup(
    name = "Student Companion App",
    options = {"build_exe":{"packages":["tkinter","urllib.request","sys","math","PIL"], "include_files":["math.ico","math.png",
    "areacirc.png","areacyn.png","aretriangle.png",
    "cone.png","pericircle.png","perirect.png","peritriangle.png","volcirc.png"
    ,"volpyr.png"]}},
    version = "0.02",
    description = "App for calculating a triangle's perimeter and area",
    executables = executables
    )


    bro this is my setup.py file but i keep getting an error when i want to create a shorcut.

    ReplyDelete
    Replies
    1. Please let me know what is the error? and provide file name and folder name without space("Perimeter App.py") or provide with escaping char so module can read the file properly.

      Delete
  2. Thanks for sharing this useful info. Check it on: Impact Windows

    ReplyDelete