Can\'t run QT based GUI application bundled by pyinstaller, the console output shows it is due to an import error:
ImportError: unable to find
I solved this for myself by commenting all in site-packages\PyQt5\__init__.py
and adding this code from an older version of QT __init__.py
:
import os as _os
_path = _os.path.dirname(__file__) + '\\Qt\\bin;' + _os.environ['PATH']
_os.environ['PATH'] = _path
As detailed in the question, when startup the bundled application in the conda console, it runs properly, all the loaded DLLs, exported by ProcessExplorer
, are in the dist dir which was created by pyinstaller. So the problem is that the path, containing pyqt DLLs, is not in the system's PATH
environment. Maybe this is a bug of pyinstaller. The Solution is add the program path to system PATH
env manually.
Here is the code snip i am using:
# Fix qt import error
# Include this file before import PyQt5
import os
import sys
import logging
def _append_run_path():
if getattr(sys, 'frozen', False):
pathlist = []
# If the application is run as a bundle, the pyInstaller bootloader
# extends the sys module by a flag frozen=True and sets the app
# path into variable _MEIPASS'.
pathlist.append(sys._MEIPASS)
# the application exe path
_main_app_path = os.path.dirname(sys.executable)
pathlist.append(_main_app_path)
# append to system path enviroment
os.environ["PATH"] += os.pathsep + os.pathsep.join(pathlist)
logging.error("current PATH: %s", os.environ['PATH'])
_append_run_path()
Assuming you don't absolutely need PyQt5 version 5.13.0, the easiest fix is to simply downgrade PyQt5 to version 5.12.2 using:
pip install pyqt5==5.12.2
and your executable will work as expected.
I comment all content of file site-packages\PyQt5\__init__.py
and install again. It works.
I use python3.5
with PyInstaller=3.5
and PyQt5=5.13.0
.The packaged exe is working on my computer, but does not work on others. The error message is:
File "anaconda3\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 627, in exec_module
File "site-packages\PyQt5\__init__.py", line 41, in <module>
File "site-packages\PyQt5\__init__.py", line 33, in find_qt
ImportError: unable to find Qt5Core.dll on PATH
So I see site-packages\PyQt5\__init__.py
(5.13.0) is:
def find_qt():
import os
path = os.environ['PATH']
dll_dir = os.path.dirname(__file__) + '\\Qt\\bin'
if os.path.isfile(dll_dir + '\\Qt5Core.dll'):
path = dll_dir + ';' + path
os.environ['PATH'] = path
else:
for dll_dir in path.split(';'):
if os.path.isfile(dll_dir + '\\Qt5Core.dll'):
break
else:
raise ImportError("unable to find Qt5Core.dll on PATH")
try:
os.add_dll_directory(dll_dir)
except AttributeError:
pass
find_qt()
del find_qt
I think PyQt5 can't find the PATH in the other computer although Qt5Core.dll
already exists in the
project directly. So I commented out the file and now it works.
Ran into the same issue after upgrade to Qt5.13.
Found this solution on pyinstaller github
You need to modify the .spec file and put the following:
datas=[(HOMEPATH + '\\PyQt5\\Qt\\bin\*', 'PyQt5\\Qt\\bin')],