Create Executable File With cx_Freeze, PyInstaller, py2exe
In this quick guide we will learn how to use cx_Freeze, PyInstaller and py2exe, the main tools for packaging Python applications into executable files. While cx_Freeze and PyInstaller are cross-platform, py2exe can only output Windows executable files.
cx_Freeze
Supported versions: Python 2.6+, 3.x. Platforms: all supported by Python.
The easiest method to get an executable file is to use the cxfreeze
script.
cxfreeze main.py
Or
python cxfreeze main.py
This simple command calls cx_Freeze to transform the main.py
file (along with all the modules it imports) into an executable file.
On Microsoft Windows systems, cxfreeze
is located inside the Scripts
folder in the Python installation directory.
To set an image as an icon, the --icon
option must be used.
cxfreeze --icon=file.ico main.py
On Microsoft Windows, the console can be hidden by passing the --base-name=Win32GUI
option.
cxfreeze --base-name=Win32GUI --icon=file.ico main.py
The script will automatically create a file called main.spec
, which contains the general options for cx_Freeze, and the dist
folder, where the executable file is created.
PyInstaller
Supported versions: 2.6+, 3.x. Platforms: Microsoft Windows and Linux.
The pyinstaller.py
file is invoked and the name of the main file to be converted is specified.
pyinstaller.py main.py
Like cx_Freeze, the --icon
option is used to indicate an icon.
pyinstaller.py --icon=file.ico main.py
The --noconsole
option logically allows you to hide the console.
pyinstaller.py --noconsole --icon=file.ico main.py
PyInstaller provides the ability to include all dependencies in a single file via the --onefile
option.
pyinstaller.py --onefile --noconsole --icon=file.ico main.py
It should be noted that this option will result in a longer program initialization time.
Additionally, the size of the final file can be reduced by compressing it with UPX via the --upx-dir
option, which you must specify the folder where the compressor is located (for example, --upx-dir=C:/upx
.)
Finally, PyInstaller can encrypt Python source code files as long as a 16-digit key is provided via --key=16_digit_key
. This option requires the pycrypto.
Upon completion, the executable file will be found in the dist
folder (along with all its dependencies, if the --onefile
option is not specified). The build
folder created during PyInstaller execution can be removed.
py2exe
Supported versions: 2.x, 3.x. Platforms: Microsoft Windows.
It is an extension to distutils
that adds the py2exe
option.
A file called setup.py
is created, indicating the name of the main file.
from distutils.core import setup import py2exe setup( console=[{"script": "main.py"}] )
This is then called with the py2exe
option.
python setup.py py2exe
Like PyInstaller, the executable file will be found in the dist
folder along with its dependencies, while build
can be left out.
To indicate an image as an icon, the icon_resources
option must be used.
setup( console=[{"script": "main.py", "icon_resources": [(1, "icon.ico")]}] )
Only in Python 3 can all dependencies be included in a single file, similar to PyInstaller's --onefile
option.
setup( options={"py2exe": {"bundle_files": 0}}, console=[{"script": "main.py", "icon_resources": [(1, "icon.ico")]}] )
Comments