Packaging PyQt5 Applications as Windows Executables with PyInstaller
Prerequisites
Ensure Python 3 is installed alongside the necessary packages. The core requirements include PyQt5 for the GUI framework, PyInstaller for binary generation, and optionally Pillow for image manipulation.
pip install pyqt5 pyqt5-tools pyinstaller pillow
PyQt5-tools facilitates conversion of Qt Designer .ui files to Python scripts, while Pillow manages image resources and application icons. Note that PyQt5 includes QtSql for database operations, eliminating the need for separate sqlite3 installation in many cases.
PyInstaller Fundamentals
PyInstaller transforms Python scripts into self-contained executables. After installation, review available options:
pyinstaller --help
Key parameters for Windows deployment:
-For--onefile: Bundles everything into a single executable-wor--windowed: Suppresses console window for GUI applications-i <path>: Embeds a custom icon (typically 128x128 or 256x256 ICO format)-p <path>: Includes addditional import paths for local modules--hidden-import <module>: Forces inclusion of modules not detected automatically
When incorporating local resource modules, combine -p and --hidden-import:
pyinstaller -i app_icon.ico -w -F main.py -p resources.py --hidden-import resources
Specification File Configuration
Alternatively, generate a specification file for granular control:
pyi-makespec main.py
Modify the generated .spec file to customize the build:
# -*- mode: python -*-
block_cipher = None
analysis = Analysis(
['main.py'],
pathex=['resources.py', 'C:\\Projects\\MyApplication'],
binaries=[],
datas=[],
hiddenimports=['resources'],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False
)
pyz = PYZ(analysis.pure, analysis.zipped_data, cipher=block_cipher)
exe = EXE(
pyz,
analysis.scripts,
analysis.binaries,
analysis.zipfiles,
analysis.datas,
[],
name='MyApp',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
runtime_tmpdir=None,
console=False,
icon='app_icon.ico'
)
Execute the build using the specification:
pyinstaller main.spec
The Analysis block defines entry points and dependencies, while EXE configures the output binary properties. Setting console=False eliminates the terminal window, and icon specifies the executable's visual identifier in Windows Explorer.