Generating Executable EXE Programs on Windows with Python

Introduction

A small program was created using Python, intended for use on Windows. This demo documents the implementation process.

The library used for packaging is the third-party Python library PyInstaller.

Content Overview

  1. Packaging with PyInstaller
  2. Testing for usability

Steps to Operate

1. Packaging with PyInstaller

  1. Install the pyinstaller dependency
pip install pyinstaller
  1. Package a single script
pyinstaller your_script.py
  1. This will create the following content in the current directory:
dist/your_script/      : Contains the packaged executable file
build/                : Intermediate files during the build process
your_script.spec      : Packaging configuration file
your_script.exe (Windows) or your_script (Linux/macOS): Executable file located in dist/your_script/
  1. Common parameters

Generating Executable EXE Programs on Windows with Python

  1. I need to build a single executable program, use the following command to build
pyinstaller -F -c your_script.py

Generating Executable EXE Programs on Windows with Python

  1. After building, we enter the folder to run the test

Generating Executable EXE Programs on Windows with Python

2. Testing Execution

The packaged program was tested on a Windows server. This website was previously generated using AI, and the results were also successful.

Generating Executable EXE Programs on Windows with Python

Conclusion

  1. No installation of Python runtime environment required The generated executable file already includes the Python interpreter and all dependencies, so the target machine does not need to have Python installed beforehand, nor does it need to executepip install.
  2. Single file distribution (One-file) By using the-F/–onefile parameter, all content can be packaged into a single executable file, simplifying file management and distribution processes; users only need to download one file to run.
  3. Automatic dependency collection PyInstaller scans theimport statements in the script and automatically packages the required third-party packages and dynamic link libraries, reducing runtime errors caused by missing dependencies.
  4. Cross-platform support Supports Windows, Linux, and macOS platforms (packages the executable file for the corresponding platform on the respective system), facilitating distribution across different operating systems.
  5. Flexible configuration of .spec files The generated.spec file can be manually edited, supporting custom packaging processes: adding extra data files, icons, hiding the console, customizing runtime options, etc.
  6. Resource files packaged together Supports packaging configuration files, images, audio, and video static resources into the executable file or the same directory using–add-data or specifying in the.spec.
  7. Support for Hook mechanism For third-party libraries involving dynamic loading or non-standard imports (such as PyQt5, wxPython, TensorFlow, etc.), Hook scripts can be used to manually specify hidden imports or additional resource dependencies to ensure complete packaging.
  8. Simplified deployment and version management Packages the application and its dependencies into an independent package, facilitating automated builds, version archiving in CI/CD, and sharing with team members or clients.
  9. Some degree of source code protection Although the packaged files can still be reverse-engineered, the source code is not stored in plain text in the.py format, providing a certain level of “packaging” for general users.
  10. Active community and documentation PyInstaller has comprehensive official documentation and an active community, allowing for quick solutions to packaging issues.

Leave a Comment