Packaging Python Scripts into Executable Files with Configurable Settings
Python applications can be distributed in various forms depending on the target audience's technical expertise.
The most common distribution methods include:
.pyfiles: Suitable for open-source projects where source code visibility is acceptable. Users must have Python installed along with required libraries..pycfiles: Used when source code protection is necessary. These are compiled bytecode files that are platform-independent but still require a compatible Python runtime.- Executable binaries: Ideal for end-users who may not be familiar with Python environments. These packages simplify installation and execution but require platform-specific builds.
Packaging Python scripts into executables can be achieved using tools like PyInstaller.
Installing PyInstaller
To install PyInstaller via pip:
pip install pyinstaller
For manual installation from source:
python setup.py install
Confirm successful installation:
pyinstaller --version
Basic Usage of PyInstaller
The basic command syntax is:
pyinstaller [options] script [script...]
To create an executable from myscript.py:
pyinstaller myscript.py
This generates two directories: build and dist. The dist folder contains the executable and associated dependencies. For cleaner distribution, use the single-file mode:
pyinstaller -F myscript.py
In this case, only one executable file is produced in the dist directory.
When dealing with configuration files that need to remain accessible, ensure all paths within the script are absolute. Otherwise, the executable will fail to locate these resources at runtime.
Advanced Options
PyInstaller also provides several utility commands:
pyi-archive_viewer: Inspect contents of packaged archives.pyi-bindepend: Analyze dynamic libray dependencies.
A .spec file is automatically generated during packaging. It defines how PyInstaller processes the application and can be manually edited for advenced customization.
To generate a spec file:
pyi-makespec options script [script...]
Then build using:
pyinstaller specfile
Or:
pyi-build specfile
How PyInstaller Works
PyInstaller bundles the Python interpreter together with your application code into a single executable. This process does not compile Python code into native machine code; thus, performance improvements are not expected. Instead, it simplifies deployment by eliminating the need for users to install Python or its dependencies.
On Unix-like systems, it relies on tools such as ldd and objdump to resolve dependencies.
Common Packaging Challenges
Hiding Console Window
To prevent a console window from appearing when running the executable:
- If using a
.specfile, setconsole=False. - Alternatively, pass the
--noconsoleflag:
pyinstaller --noconsole myscript.py
For a single-file executable:
pyinstaller -F --noconsole myscript.py
Custom Output Directory
Use the -p option to specify additional paths for module loading and -i for custom icons:
pyinstaller -F -w -p D:\tmp\core-python\libs -i D:\tmp\main.ico main.py
Options:
-F: Create a single executable file.-Dor--onedir: Generate a directory containing the executable and dependencies (default).-wor--windowed: Hide console window (useful for GUI apps).-cor--console: Show console window (default behavior).-p: Add directories to Python path.-i: Set icon for the executable.