Python is an interpreted language. This implies that users of our console or desktop applications (for example, those developed with tkinter, PyQt/PySide, wxPython, etc.) must have Python installed to run
.py files. However, there are multiple tools that allow us to create executable files (
.app, etc.) for Windows, Linux and Mac systems, so that our application can be executed without requiring the installation of Python. This makes it much easier to distribute our programs and improves user experience.
To get you started quickly, in this article we will briefly introduce the three main tools for creating executables and how to use them: PyInstaller, cx_Freeze and py2exe (the latter only for Windows). If you don't know which one to choose, just use PyInstaller.
PyInstaller can generate executables for Windows, Linux and Mac with a simple command. First, let's make sure to install PyInstaller via pip:
Then, invoke the following command from the terminal in order to convert the
main.py file to an executable:
If your application is made up of multiple
.py files, just pass the main file (i.e., the file where the code to start the program lies.) The remaining files will be discovered by PyInstaller.
After running the command, the executable file will be found in the
dist folder along with all its dependencies. The format and extension of the generated executable file will depend on the operating system from which the command was executed. On Windows we will get a
.exe file, on Mac a
.app file, and on Linux just an executable file without an extension. The
build folder created during PyInstaller execution can be removed.
If you want PyInstaller to generate a single executable with no dependencies (files
.so, etc.), pass the
Executables built with
--onefile are easier to distribute, but take longer to initialize because dependencies must be extracted to a temporary folder before running the program.
For GUI applications, it will be better to tell PyInstaller not to show the console via the
--noconsole option (options are combinable):
By default the generated executables have the PyInstaller icon. To specify your own icon, use the
--icon option followed by the path or name of the icon file:
The size of the final file can be reduced by compressing it with UPX via the
--upx-dir option, by which you must specify the directory where compressor is installed. For example:
Finally, PyInstaller can encrypt Python source code files as long as a 16-digit key is provided via
This option requires the tinyaes package, installable via:
cx_Freeze is very similar to PyInstaller, and can also generate executables for Windows, Linux, and Mac. You can install cx_Freeze via pip by running:
Once installed, if the main file of our application is
main.py, open a terminal where it is located and run:
-c option specifies the name of the file to convert and
--target-dir the directory where cx_Freeze will store the executable file.
To set an image as an icon, use the
On Windows, the console can be hidden by adding the
--base-name=Win32GUI option (options are combinable).
To generate an executable file, first create a file called
setup.py like this:
Then invoke it with the
Like PyInstaller, the executable file will be found in the
dist folder along with its dependencies, while
build can be safely deleted.
For GUI applications, use the
windows argument instead of
console, to avoid showing the console:
To configure an icon image, use the
All dependencies can be included in a single file, similar to PyInstaller's
--onefile option, by using: