Create a python executable and MSI installer using Cx_freeze

Python is cool, easy to use, and one of the most popular programming languages out there. But python has a big downside when it comes to running scripts.

First of all, a normal user doesn’t have python installed on his machine. Second of all, it’s tricky for a simple user to know how to run py scripts (or maybe he is not comfortable).

Because Python is so popular and has a certain age, many modules or tools have been developed for it. Nowadays, you can use a simple tool like Auto PY to EXE to convert your py scripts into an executable. With this method, you are guaranteed that your scripts will work on every user’s machine out there, because these tools put all the necessary python runtime files near the script, converts the script to an EXE and runs it.

Basically, what these converters do is take your py script, place the py installation and necessary 3rd party modules near your py script, and when you launch that resulted EXE, the exe calls python.exe with your script.

However, you still need to distribute your exe somehow, and that is where CX_FREEZE comes into play. Not only cx_freeze converts your py script into an exe, you have the option to create an MSI which you can give your users to install.

Install CX_FREEZE

To install cx_freeze is simple. Open up a cmd and type the following command:

pip install cx_Freeze --upgrade

Create script

In order to convert your py script to exe and create an MSI installer, you will need to create a python script. The script is as follows:

import sys
from cx_Freeze import setup, Executable

company_name = ‘My Company Name’
product_name = ‘My Gui’

bdist_msi_options = {
‘upgrade_code’: ‘{48B079F4-B598-438D-A62A-8A233A3F8901}’,
‘add_to_path’: False,
‘initial_target_dir’: r'[ProgramFilesFolder]\%s\%s’ % (company_name, product_name),
}

# GUI applications require a different base on Windows
base = None
if sys.platform == ‘win32’:
base = ‘Win32GUI’

exe = Executable(script=’helloword.py’,
base=base,
)

setup(name=product_name,
version=’1.0.0′,
description=’blah’,
executables=[exe],
options={‘bdist_msi’: bdist_msi_options})

Change the helloworld.py script with your script name. If you have any modules that you need to add, there are some changes to the script you need to make.

Let’s assume we need to add the scrapy and Twisted modules, in this case, the script would look like this:

import sys
from cx_Freeze import setup, Executable

company_name = ‘My Company Name’
product_name = ‘My Gui’

bdist_msi_options = {
‘upgrade_code’: ‘{48B079F4-B598-438D-A62A-8A233A3F8901}’,
‘add_to_path’: False,
‘initial_target_dir’: r'[ProgramFilesFolder]\%s\%s’ % (company_name, product_name),
}

build_exe_options = {
‘includes’: [‘scrapy’, ‘Twisted’],
}

# GUI applications require a different base on Windows
base = None
if sys.platform == ‘win32’:
base = ‘Win32GUI’

exe = Executable(script=’helloword.py’,
base=base,
)

setup(name=product_name,
version=’1.0.0′,
description=’blah’,
executables=[exe],
options={‘bdist_msi’: bdist_msi_options,
‘build_exe’: build_exe_options})

Build EXE and MSI

Once the script is done, the last step is to compile everything. To do this, run the following command:

python setup.py bdist_msi

Once the build is finished, you will find the resulted MSI in the “dist” folder. The MSI will contain the converted python script (to exe) and all the necessary modules/runtime for python. All you have to do is give this MSI to your users to install it.

If you want to modify your MSI and add shortcuts, registry, or other necessary adjustments, you can use a tool like Advanced Installer to achieve this.

Sharing is caring!

Leave a comment

Your email address will not be published. Required fields are marked *

two × 5 =