Various Methods to Package Python Files (.py) into Executable Files (.exe)

Introduction

There are various methods to package Python files (.py) into executable files (.exe), each corresponding to different libraries in Python. The four commonly used libraries are: PyInstaller, cx_Freeze, Py2exe, and Nuitka. Each has its own characteristics and limitations, and we can choose the appropriate method based on our needs.

1. Using the PyInstaller Library (Simplest and Most Common)

PyInstaller is a popular tool for packaging Python programs into standalone executable files (.exe files). Below is a detailed explanation and examples of how to use PyInstaller to package .py files into .exe files, along with an introduction to commonly used parameters in this method.

1.2 Common Parameters and Usage

Example 1: Direct Packaging

Command:<span>pyinstaller py_file</span>; for example: pyinstaller test.py

Open the cmd command window on your computer, enter the command, and a dist directory will be generated, which contains the executable file and some dependency files. By default, the name of the executable file is the same as the script name.

Example 2: Package as a Single Executable File (-F)

Command:<span>pyinstaller -F test.py</span>

This packages all files into a single executable file. By default, PyInstaller generates a folder containing the executable file and related dependency files. With this option, all files will be packaged into one file.

Example 3: Close Command Line Window (-w)

<span><span>Command:</span></span><code class="language-plaintext"><span>pyinstaller -w test.py</span>

On Windows, the default Python program will have a command line window. If the program is a GUI program, using this option can prevent the command line window from appearing. This is commonly used for GUI programs like PyQt or Tkinter. Note: When packaging TK program code, if this option is not added, the packaged program may not execute.

Example 4: Set Executable File Icon (-i)

pyinstaller -i icon.ico test.py

<span><span>Set the icon for the executable file. You can provide an icon file in .ico format. This option is usually used to customize the appearance of the application.</span></span>

1.3 Other Parameters (For Understanding)

1. –name

Command: pyinstaller –name myapp test.py

Specify the name of the generated executable file.

2. –add-data

Command:

pyinstaller –add-data=”datafile.txt;data” test.py # Windows

pyinstaller –add-data=”datafile.txt:data” test.py # Linux/macOS

Add external files or folders to the packaged executable file. SRC is the source file path, and DEST is the target path. This option can be used multiple times for multiple files. On Windows, SRC;DEST is separated by a semicolon (;); on Linux and macOS, it is separated by a colon (:).

3. –hidden-import

Command: pyinstaller –hidden-import=my_module test.py

This is used to specify modules that PyInstaller should automatically ignore during program analysis. Typically, if the code dynamically imports modules, PyInstaller may not automatically detect these modules and needs to be explicitly added.

4. –clean

Command: pyinstaller –clean test.py

This clears temporary files from the last build process, ensuring a fresh build from scratch. This option helps resolve some caching issues, especially when packaging after modifying code.

5. –distpath

Command: pyinstaller –distpath=output test.py

Specify the directory where the generated executable file will be located. By default, PyInstaller creates a dist folder in the current directory.

6. –workpath

Command: pyinstaller –workpath=build test.py

Specify the location of the PyInstaller working directory. By default, it generates a build folder to store intermediate files.

7. –specpath

Command: pyinstaller –specpath=specs test.py

This is used to specify the directory for the .spec file. The .spec file contains configuration information for the packaging process, and it can usually be modified to customize PyInstaller’s behavior.

8. –no-upx

Command: pyinstaller –no-upx test.py

This disables UPX compression. UPX (Ultimate Packer for eXecutables) is an executable file compression tool that PyInstaller uses by default to compress executable files. If compatibility issues arise on certain systems, UPX compression can be disabled.

9. –debug

Command: pyinstaller –debug test.py

This enables debug mode, which can help see more debug information in the packaged application, commonly used for troubleshooting.

10. –strip

Command: pyinstaller –strip test.py

This removes unnecessary debug symbols, reducing the size of the executable file.

Leave a Comment