Python

How to distribute python applications

Convert Python applications to an executable program. (Such as exe Windows, dmg Mac, rpm Linux).
This tutorial uses pyinstaller to create an executable for your Python application and allow to distribute your Python application more easily. If you use Pyinstaller then you do not need to have an python installer installed to run the created exe.
Pyinstaller
If you do want an alternative which has also an included Python interpreter, optimized executable file size and more advanced features, then you can also use "Cx_freeze", which is discussed below in an own part of this tutorial.
  •  Install pyinstaller (creates standalone executables from Python scripts)
python -m pip install pyinstaller

  • Move to the root folder of your application, where you have your main python program. And run this application to create an executable runnable on the operating system on which this command was run.
pyinstaller --onefile --windowed --add-data="MyFolder/;." --icon="icon.ico" MyApp.py​

This command creates a runnable python application which has a gui (command: windowed). You can add folders or files with the command add-data. E.g.: --data="SourceFolder;DestinationFolder". We use here point "." to tell Pyinstaller to save the folder MyFolder in the root directory of our application.
You have to run pyinstaller on all operating systems where you want to distribute your application.

  • The file will be saved in a folder named "Output" which is in the directory where you did run Pyinstaller.
You may have to copy other folders in the folder where you run this new created exe application.

  • Create an installer for this application
Now you can create an installer which can install this application. If you want to distribute in Windows it is recommended to use the application "Inno Setup" to create an installer for your new created exe application. Inno Setup has a wizard which allows you to create the installer more easily without writing code or a setup.py file. After you saved your Inno Setup project (iss file) you can edit it through the built in editor.

Official website of Inno Setup:
http://www.jrsoftware.org/isdl.php

More information and documentation about PyInstaller:
https://pyinstaller.readthedocs.io/en/stable/
Alternative Option: cx_freeze
You can also use cx_freeze instead of pyinstaller. For this method: An installed Python environment/interpreter is needed before you can run your created exe file.

  • Install cx_freeze  (creates standalone executables from Python scripts)
python -m pip install cx_Freeze​

  • Create the file setup.py (installation file)
The setup.py file looks like this. You have to adjust it to your python application.
import sys
from cx_Freeze import *

# Dependencies are automatically detected, but it might need fine tuning.
#build_exe_options = {"packages": ["os"], "excludes": ["tkinter"]}

#Include Folders
buildoptions = dict(include_files = ['MyApp/'])
#buildOptions = dict(include_files = [(absolute_path_to_your_file,'final_filename')]) #single file, absolute path.

# GUI applications require a different base on Windows (the default is for a
# console application).
base = None
if sys.platform == "win32":
    base = "Win32GUI"

setup(  name = "MyApp",
        version='1.0',
        description='My App description',
        author='Author',
        author_email='[email protected]',
        url='https://URL',
        long_description='This is a application description.',
        options=dict(build_exe = buildoptions),
        executables = [Executable("MyApp\myapp.py",
        shortcutName="My App",
        shortcutDir="MyApp",
        icon="icon.ico",
        base=base)])​


More information on how to set up a short cut (in the Windows programm list) for this application:
https://stackoverflow.com/questions/15734703/use-cx-freeze-to-create-an-msi-that-adds-a-shortcut-to-the-desktop

  • Put the setup.py in the root folder of the application folder
Put in there also folders/packages that you need to run your python application.

  • To create an executable exe file for the computer where this command is run:
python setup.py build

It is recommended to use "Inno Setup" to create an installable exe program for the created runnable exe file. This program allows you to create an installable exe file with menu short cuts more easily in Windows. You have to adjust the setup.py file if you use the command "bdist --format msi".

  • Create an installer for your python application through cx_freeze:
python setup.py bdist --format=msi​


Please check also the documentation for more information on cx_freeze:
https://cx-freeze.readthedocs.io/en/latest/distutils.html
Cookies make it easier for us to provide you with our services. With the usage of our services you permit us to use cookies.
Ok