Fading Coder

One Final Commit for the Last Sprint

Home > Notes > Content

Packaging PyQt5 Applications as Windows Executables with PyInstaller

Notes 1

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:

  • -F or --onefile: Bundles everything into a single executable
  • -w or --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.

Related Articles

Deploying a Maven Web Application to Tomcat 9 Using the Tomcat Manager

Tomcat 9 does not provide a dedicated Maven plugin. The Tomcat Manager interface, however, is backward-compatible, so the Tomcat 7 Maven Plugin can be used to deploy to Tomcat 9. This guide shows two...

Skipping Errors in MySQL Asynchronous Replication

When a replica halts because the SQL thread encounters an error, you can resume replication by skipping the problematic event(s). Two common approaches are available. Methods to Skip Errors 1) Skip a...

Spring Boot MyBatis with Two MySQL DataSources Using Druid

Required dependencies application.properties: define two data sources and poooling Java configuration for both data sources MyBatis mappers for each data source Controller endpoints to verify both co...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.