SoFunction
Updated on 2025-03-04

PyInstaller converts Python scripts into standalone executable files

Introduction to PyInstaller

PyInstaller is a cross-platform tool whose main functions include:

  • Convert Python scripts to standalone executable files.
  • Supports Windows, macOS and Linux systems.
  • Automatically detect and package dependencies of Python scripts.
  • The generated files can be in a single file form (such as.exe), or it can be a directory containing multiple dependencies.

Install PyInstaller

Use pip to install PyInstaller:

pip install pyinstaller

Check whether the installation is successful:

pyinstaller --version

Basic usage

Convert scripts to executable files

Suppose your script is named, you can run the following command:

pyinstaller 

When done, PyInstaller generates the following:

  • dist/: Store the final executable file.
  • build/: Store temporary files during packaging.
  • : PyInstaller configuration file.

The generated executable file is located atdist/appIn the directory.

Generate a single file executable program

By default, PyInstaller generates a directory containing multiple files. If you want to generate a separate single-file executable file, you can add--onefileParameters:

pyinstaller --onefile 

Add an icon

Can be passed--iconParameters add custom icons to the program (support.icoFormat):

pyinstaller --onefile --icon= 

Advanced usage

Hide console window

For graphical applications, you can hide the console window that pops up at runtime and use--noconsoleParameters:

pyinstaller --onefile --noconsole 

Custom output directory

Can be passed--distpathand--workpathParameters specify the output directory and temporary file directory:

pyinstaller --onefile --distpath ./output --workpath ./temp 

Use .spec files

Generated by PyInstaller.specA file is a configuration script that contains all parameters in the packaging process. You can edit this file and repackage it with the following command:

pyinstaller 

Frequently Asked Questions

1. Executable file is too large

PyInstaller packages all dependencies, resulting in the generated executable file being large in size. You can try the following methods to optimize:

  • useUPXThe tool compresses the executable file:
pyinstaller --onefile --upx-dir=/path/to/upx 
  • Remove unnecessary dependencies.

2. The program cannot run after packaging

Possible reasons:

  • The dynamic link library is missing (such as.dll.sodocument). Check if the external dependencies required by the program are installed.
  • Use libraries that are not fully supported by PyInstaller. Can be passed--hidden-importManually add hidden dependencies:
pyinstaller --onefile --hidden-import=<module_name> 

3. Slow packaging speed

For complex projects, packaging can take a long time. Can be used--cleanParameters to clean temporary files to speed up subsequent packaging speed.

Comparison of PyInstaller with other tools

tool advantage shortcoming
PyInstaller Cross-platform, support multiple modes Packaging files are large and rely on Python environment
cx_Freeze Supports more fine-grained packaging control Complex configuration
py2exe Focus on Windows Platform Only supported on Windows
py2app Focus on macOS platform Only support macOS

Example: Complete Packaging Command

The following command will write the scriptPackage as a single file executable program with icons and hide the console window:

pyinstaller --onefile --icon= --noconsole 

Summarize

PyInstaller is a very practical tool for developers who need to distribute Python applications. Through PyInstaller, Python programs can be turned into independent executable files, eliminating the user's trouble of configuring the environment.

The above is the detailed content of PyInstaller converting Python scripts into independent executable files. For more information about PyInstaller Python scripts into executable files, please follow my other related articles!