1

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

  1. Added everying that seemed relevant to the hiddenimports list in main.spec
  2. Created hooks\hook-qtpy.py
  3. 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
  4. Ran poetry run python -c "import qtpy; import PyQt6; print(qtpy.API_NAME)" from within the virtual environment and confirmed it did print "PyQt6"
  5. Added collect_dynamic_libs("PyQt6") to main.spec’s binaries list
  6. Removed it because it threw ValueError: not enough values to unpack (expected 2, got 0) when trying to generate the EXE
  7. Ran pyi-archive_viewer main.exe and confirmed that PyQt6 was bundled
  8. 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 --onefile is not possible."
  9. Generated a new main.spec by running Pyinstaller on main.py with the --clean option and without the --onefile option
  10. Added the previous hiddenmodules, pathex, hookspath and excludes into the new main.spec
  11. Gave ChatGPT a list of the things I tried and asked for advice
  12. Followed ChatGPT’s advice to add collect_data_files("PyQt6") to main.spec’s datas
  13. Removed collect_data_files("PyQt6") because Pyinstaller threw ValueError: not enough values to unpack (expected 2, got 0) when trying to generate the EXE
  14. Followed ChatGPT’s advice to add collect_submodules("qtpy") + collect_submodules("PyQt6") to main.spec’s hiddenimports list
5
  • 1
    Notes: 1. The page you linked is about PySide, not PyQt, also the "Qt for Python" (aka, PySide) docs are known to be poorly maintained: AFAIK, PyInstaller fully supports both Qt bindings (PyQt and PySide) for Qt6 with the --onefile flag 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). Commented Sep 22 at 19:52
  • 1
    Do some research using relevant queries and search syntax (including PyInstaller, the onefile flag, 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. Commented Sep 22 at 19:55
  • @musicamante TY for the info. I’ll keep looking for ideas without excluding onefile and broaden my search to other errors. As for the LLM mention, it’s because I copied and pasted some notes from the project and there is a rule about AI disclosure so I mention every time I used a LLM. Commented Sep 22 at 20:17
  • If you're referring to a specific policy of the AI you're using, that's irrelevant: you were not quoting its full content, but only what you did after its suggestions; you are not expected to cite the usage of a specific AI when vaguely explaining what you tried. If you're instead referring to the AI ban policy on StackOverflow, that's even less relevant: that policy is about posts mostly written with AI generated content; considering the current [un]reliability of AIs, there is really no point in telling us that what they suggested didn't work out. Commented Oct 7 at 3:18
  • @musicamante Sorry, I realize I didn’t specify where the rule came from. I meant that the project has a rule about it because I am doing it for an academic institution with strict a AI disclosure policy. Commented Oct 7 at 12:28

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.