Summary
I am generating an EXE for a Python project using Pyinstaller.
I am using Powershell on Windows 11 and a Python 3.11 in a Poetry virtual environment.
I keep encountering the following error when running the EXE:
Traceback (most recent call last):
File "main.py", line 7, in <module>
import qtmodern.styles
File "pyimod02_importers.py", line 457, in exec_module
File "qtmodern\styles.py", line 3, in <module>
File "pyimod02_importers.py", line 457, in exec_module
File "qtpy\__init__.py", line 293, in <module>
qtpy.QtBindingsNotFoundError: No Qt bindings could be found
[PYI-44852:ERROR] Failed to execute script 'main' due to unhandled exception!
I have tried multiple things and none has worked so far. I would very much appreciate any advice on what to try next.
Code
I use this command to generate an EXE:
poetry run pyinstaller main.spec --clean
I use the following files:
#main.spec
# -*- mode: python ; coding: utf-8 -*-
a = Analysis(
['main.py'],
pathex=['fullpathonmypc\\.venv\\Lib\\site-packages'],
binaries=[],
datas=[],
hiddenimports= ["qtmodern", "qtmodern.styles", "qtmodern.windows",
"qtpy", "qtpy.QtCore", "qtpy.QtGui", "qtpy.QtWidgets",
"PyQt6.QtCore", "PyQt6.QtGui", "PyQt6.QtWidgets",
"PyQt6.QtPrintSupport", "PyQt6.QtSvg", "PyQt6.QtOpenGL", "event_connector", "numpy"],
hookspath=[],
hooksconfig={},
runtime_hooks=[],
excludes=['PySide6'],
noarchive=False,
optimize=0,
)
pyz = PYZ(a.pure)
exe = EXE(
pyz,
a.scripts,
[],
exclude_binaries=True,
name='main',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
console=True,
disable_windowed_traceback=False,
argv_emulation=False,
target_arch=None,
codesign_identity=None,
entitlements_file=None,
)
coll = COLLECT(
exe,
a.binaries,
a.datas,
strip=False,
upx=True,
upx_exclude=[],
name='main',
)
#hooks\hook-qtmodern.py
from PyInstaller.utils.hooks import collect_submodules
hiddenimports = collect_submodules("qtmodern")
#hooks\hook-qtpy.py
from PyInstaller.utils.hooks import collect_submodules
hiddenimports = collect_submodules("qtpy")
Previous troubleshooting
- Added everying that seemed relevant to the hiddenimports list in main.spec
- Created
hooks\hook-qtpy.py - Added the line
os.environ["QT_API"] = "pyqt6"before the line that triggers the error in main.py. The only imports before it are sys, shutil and os - Ran
poetry run python -c "import qtpy; import PyQt6; print(qtpy.API_NAME)"from within the virtual environment and confirmed it did print "PyQt6" - Added
collect_dynamic_libs("PyQt6")to main.spec’s binaries list - Removed it because it threw
ValueError: not enough values to unpack (expected 2, got 0)when trying to generate the EXE - Ran
pyi-archive_viewer main.exeand confirmed that PyQt6 was bundled - Did more research and found a page from Qt for Python’s documentation indicating that "As of March 2021, Qt 6 is not supported yet. PyInstaller is unable to properly deploy Qt; the Qt plugins are not copied. With that, using
--onefileis not possible." - Generated a new main.spec by running Pyinstaller on main.py with the
--cleanoption and without the--onefileoption - Added the previous hiddenmodules, pathex, hookspath and excludes into the new main.spec
- Gave ChatGPT a list of the things I tried and asked for advice
- Followed ChatGPT’s advice to add
collect_data_files("PyQt6")to main.spec’s datas - Removed
collect_data_files("PyQt6")because Pyinstaller threwValueError: not enough values to unpack (expected 2, got 0)when trying to generate the EXE - Followed ChatGPT’s advice to add
collect_submodules("qtpy") + collect_submodules("PyQt6")to main.spec’s hiddenimports list
--onefileflag right now; 2. while using generic LLMs such as ChatGPT is a valid possibility when looking for advice and alternatives, there's no point in remarking their usage if those suggestions fail (it's just a further demonstration of the unreliability of such tools, especially for relatively niche languages/toolkits/tools).onefileflag, both PyQt and PySide, and appropriate operators like quotes and keywords) in order to find related posts that show successful builds with PyQt6 and PyInstaller with the "one file" mode, even if they're about other issues.