How to Package a PyQt5 Application into an Executable with PyInstaller
This guide walks through preparing a PyQt5 project, installing PyInstaller, generating and configuring the .spec file, and using PyInstaller commands to build a standalone Windows executable, including tips for handling console windows and embedding resources.
The author, while learning PyQt5, records a step‑by‑step tutorial on how to package a PyQt5 project into a Windows executable using PyInstaller.
Step 1 – Prepare the project files : Place all source files in a folder, with frame_main.py as the main script.
Step 2 – Install PyInstaller : Open a command prompt in the project directory and install PyInstaller (e.g., pip install pyinstaller ).
Step 3 – Generate the .spec file : Run the command pyi-makespec frame_main.py . This creates frame_main.spec , which contains the build configuration.
The generated .spec file looks like this:
<code># -*- mode: python ; coding: utf-8 -*-
block_cipher = None
a = Analysis(['frame_main.py'],
pathex=[],
binaries=[],
datas=[],
hiddenimports=[],
hookspath=[],
hooksconfig={},
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False)
pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
[],
exclude_binaries=True,
name='frame_main',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
console=True,
disable_windowed_traceback=False,
target_arch=None,
codesign_identity=None,
entitlements_file=None)
coll = COLLECT(exe,
a.binaries,
a.zipfiles,
a.datas,
strip=False,
upx=True,
upx_exclude=[],
name='frame_main')
</code>Step 4 – Configure the .spec file : Add any required data files (e.g., images) to the datas list and set the icon path (using an absolute path) in the exe configuration.
Step 5 – Build the executable : Execute pyinstaller -F -w frame_main.spec . The -F flag creates a single‑file bundle, and -w suppresses the console window.
After the build completes, a dist folder appears containing the frame_main.exe file.
If the resulting executable still shows a black console window, edit the .spec file to change console = True to console = False , then rebuild with the same PyInstaller command.
Rebuilding after this change produces an executable that runs without displaying a console window.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.