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.


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.



python cxfreeze

This simple command calls cx_Freeze to transform the 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

On Microsoft Windows, the console can be hidden by passing the --base-name=Win32GUI option.

cxfreeze --base-name=Win32GUI --icon=file.ico

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.


Supported versions: 2.6+, 3.x. Platforms: Microsoft Windows and Linux.

The file is invoked and the name of the main file to be converted is specified.

Like cx_Freeze, the --icon option is used to indicate an icon. --icon=file.ico

The --noconsole option logically allows you to hide the console. --noconsole --icon=file.ico

PyInstaller provides the ability to include all dependencies in a single file via the --onefile option. --onefile --noconsole --icon=file.ico

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.


Supported versions: 2.x, 3.x. Platforms: Microsoft Windows.

It is an extension to distutils that adds the py2exe option.

A file called is created, indicating the name of the main file.

from distutils.core import setup
import py2exe

    console=[{"script": ""}]

This is then called with the py2exe option.

python 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.

    console=[{"script": "", "icon_resources": [(1, "icon.ico")]}]

Only in Python 3 can all dependencies be included in a single file, similar to PyInstaller's --onefile option.

    options={"py2exe": {"bundle_files": 0}},
    console=[{"script": "", "icon_resources": [(1, "icon.ico")]}]