Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix "ImportError: unable to find Qt5Core.dll on PATH" after pyinstaller bundled the python application

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 Qt5Core.dll on PATH

Originally, the pyqt5/pyqt and pyinstaller is from condo. I've tried to reinstall pyqt5 and pyinstaller from pypi, but it does not solve the problem.

I checked the pyinstaller dist dir, it contains all needed dlls. When I am in conda env(Start a conda console, then run the bundled application in the console), the bundled application runs properly. I checked the loaded dlls, all of them are in the dist dir or system default dirs.

I'm on windows 10 x64 [Ver 10.0.18362.175], Python version is 3.7.3 from miniconda.

conda packages:

    # packages in environment at D:\Miniconda3:    
    #    
    # Name                    Version                   Build
    altgraph                  0.16.1                   pypi_0
    asn1crypto                0.24.0                   py37_0
    attrs                     19.1.0                   py37_1
    backcall                  0.1.0                    py37_0
    blas                      1.0                         mkl
    bleach                    3.1.0                    py37_0
    bzip2                     1.0.7                he774522_0
    ca-certificates           2019.5.15                     0
    certifi                   2019.6.16                py37_0
    cffi                      1.12.3           py37h7a1dbc1_0
    chardet                   3.0.4                    py37_1
    colorama                  0.4.1                    py37_0
    conda                     4.7.5                    py37_0
    conda-package-handling    1.3.10                   py37_0
    cryptography              2.7              py37h7a1dbc1_0
    decorator                 4.4.0                    py37_1
    defusedxml                0.6.0                      py_0
    entrypoints               0.3                      py37_0
    future                    0.17.1                   pypi_0
    icc_rt                    2019.0.0             h0cc432a_1
    icu                       58.2                 ha66f8fd_1
    idna                      2.8                      py37_0
    intel-openmp              2019.4                      245
    ipykernel                 5.1.1            py37h39e3cac_0
    ipython                   7.6.0            py37h39e3cac_0
    ipython_genutils          0.2.0                    py37_0
    ipywidgets                7.4.2                    py37_0
    jedi                      0.13.3                   py37_0
    jinja2                    2.10.1                   py37_0
    jpeg                      9b                   hb83a4c4_2
    jsonschema                3.0.1                    py37_0
    jupyter                   1.0.0                    py37_7
    jupyter_client            5.2.4                    py37_0
    jupyter_console           6.0.0                    py37_0
    jupyter_core              4.5.0                      py_0
    jupyterlab                0.35.4           py37hf63ae98_0
    jupyterlab_server         0.2.0                    py37_0
    libarchive                3.3.3                h0643e63_5
    libiconv                  1.15                 h1df5818_7
    libpng                    1.6.37               h2a8f88b_0
    libsodium                 1.0.16               h9d3ae62_0
    libxml2                   2.9.9                h464c3ec_0
    lz4-c                     1.8.1.2              h2fa13f4_0
    lzo                       2.10                 h6df0209_2
    m2w64-gcc-libgfortran     5.3.0                         6
    m2w64-gcc-libs            5.3.0                         7
    m2w64-gcc-libs-core       5.3.0                         7
    m2w64-gmp                 6.1.0                         2
    m2w64-libwinpthread-git   5.0.0.4634.697f757             
    macholib                  1.11                     pypi_0
    markupsafe                1.1.1            py37he774522_0
    menuinst                  1.4.16           py37he774522_0
    mistune                   0.8.4            py37he774522_0
    mkl                       2019.4                      245
    mkl_fft                   1.0.12           py37h14836fe_0
    mkl_random                1.0.2            py37h343c172_0
    msys2-conda-epoch         20160418                      1
    nbconvert                 5.5.0                      py_0
    nbformat                  4.4.0                    py37_0
    notebook                  5.7.8                    py37_0
    numpy                     1.16.4           py37h19fb1c0_0
    numpy-base                1.16.4           py37hc3f5095_0
    openssl                   1.1.1c               he774522_1
    pandoc                    2.2.3.2                       0
    pandocfilters             1.4.2                    py37_1
    parso                     0.5.0                      py_0
    pefile                    2019.4.18                pypi_0
    pickleshare               0.7.5                    py37_0
    pip                       19.1.1                   py37_0
    prometheus_client         0.7.1                      py_0
    prompt_toolkit            2.0.9                    py37_0
    pycosat                   0.6.3            py37hfa6e2cd_0
    pycparser                 2.19                     py37_0
    pycrypto                  2.6.1                    pypi_0
    pygments                  2.4.2                      py_0
    pyinstaller               3.4                      pypi_0
    pyopenssl                 19.0.0                   py37_0
    pyqt                      5.9.2            py37h6538335_2
    pyqt5                     5.13.0                   pypi_0
    pyqt5-sip                 4.19.18                  pypi_0
    pyrsistent                0.14.11          py37he774522_0
    pysocks                   1.7.0                    py37_0
    python                    3.7.3                h8c8aaf0_0
    python-dateutil           2.8.0                    py37_0
    python-libarchive-c       2.8                      py37_9
    pywin32                   223              py37hfa6e2cd_1
    pywin32-ctypes            0.2.0                    pypi_0
    pywinpty                  0.5.5                 py37_1000
    pyzmq                     18.0.0           py37ha925a31_0
    qt                        5.9.7            vc14h73c81de_0
    qtconsole                 4.5.1                      py_0
    requests                  2.22.0                   py37_0
    ruamel_yaml               0.15.46          py37hfa6e2cd_0
    scipy                     1.2.1            py37h29ff71c_0
    send2trash                1.5.0                    py37_0
    setuptools                41.0.1                   py37_0
    sip                       4.19.8           py37h6538335_0
    six                       1.12.0                   py37_0
    sqlite                    3.28.0               he774522_0
    terminado                 0.8.2                    py37_0
    testpath                  0.4.2                    py37_0
    tornado                   6.0.3            py37he774522_0
    tqdm                      4.32.1                     py_0
    traitlets                 4.3.2                    py37_0
    urllib3                   1.24.2                   py37_0
    vc                        14.1                 h0510ff6_4
    vs2015_runtime            14.15.26706          h3a45250_4
    wcwidth                   0.1.7                    py37_0
    webencodings              0.5.1                    py37_1
    wheel                     0.33.4                   py37_0
    widgetsnbextension        3.4.2                    py37_0
    win_inet_pton             1.1.0                    py37_0
    wincertstore              0.2                      py37_0
    winpty                    0.4.3                         4
    xz                        5.2.4                h2fa13f4_4
    yaml                      0.1.7                hc54c509_2
    zeromq                    4.3.1                h33f27b4_3
    zlib                      1.2.11               h62dcd97_3
    zstd                      1.3.7                h508b16e_0

pyinstaller dist dir files:

    PS C:\Users\xxx\Idp\dist\main> dir


        Directory: C:\Users\xxx\Idp\dist\main


    Mode                LastWriteTime         Length Name
    ----                -------------         ------ ----
    d-----         2019/7/9     15:04                Crypto
    d-----         2019/7/9     15:04                PyQt5
    -a----         2019/7/8     16:35          19208 api-ms-win-core-console-l1-1-0.dll
    -a----         2019/7/8     16:35          18696 api-ms-win-core-datetime-l1-1-0.dll
    -a----         2019/7/8     16:35          18696 api-ms-win-core-debug-l1-1-0.dll
    -a----         2019/7/8     16:35          18696 api-ms-win-core-errorhandling-l1-1-0.dll
    -a----         2019/7/8     16:35          22280 api-ms-win-core-file-l1-1-0.dll
    -a----         2019/7/8     16:35          18696 api-ms-win-core-file-l1-2-0.dll
    -a----         2019/7/8     16:35          18696 api-ms-win-core-file-l2-1-0.dll
    -a----         2019/7/8     16:35          18696 api-ms-win-core-handle-l1-1-0.dll
    -a----         2019/7/8     16:35          19208 api-ms-win-core-heap-l1-1-0.dll
    -a----         2019/7/8     16:35          18696 api-ms-win-core-interlocked-l1-1-0.dll
    -a----         2019/7/8     16:35          19720 api-ms-win-core-libraryloader-l1-1-0.dll
    -a----         2019/7/8     16:35          21256 api-ms-win-core-localization-l1-2-0.dll
    -a----         2019/7/8     16:35          19208 api-ms-win-core-memory-l1-1-0.dll
    -a----         2019/7/8     16:35          18696 api-ms-win-core-namedpipe-l1-1-0.dll
    -a----         2019/7/8     16:35          19720 api-ms-win-core-processenvironment-l1-1-0.dll
    -a----         2019/7/8     16:35          20744 api-ms-win-core-processthreads-l1-1-0.dll
    -a----         2019/7/8     16:35          19208 api-ms-win-core-processthreads-l1-1-1.dll
    -a----         2019/7/8     16:35          18184 api-ms-win-core-profile-l1-1-0.dll
    -a----         2019/7/8     16:35          19208 api-ms-win-core-rtlsupport-l1-1-0.dll
    -a----         2019/7/8     16:35          18696 api-ms-win-core-string-l1-1-0.dll
    -a----         2019/7/8     16:35          20744 api-ms-win-core-synch-l1-1-0.dll
    -a----         2019/7/8     16:35          19208 api-ms-win-core-synch-l1-2-0.dll
    -a----         2019/7/8     16:35          19720 api-ms-win-core-sysinfo-l1-1-0.dll
    -a----         2019/7/8     16:35          19208 api-ms-win-core-timezone-l1-1-0.dll
    -a----         2019/7/8     16:35          18696 api-ms-win-core-util-l1-1-0.dll
    -a----         2019/7/8     16:35          19720 api-ms-win-crt-conio-l1-1-0.dll
    -a----         2019/7/8     16:35          22792 api-ms-win-crt-convert-l1-1-0.dll
    -a----         2019/7/8     16:35          19208 api-ms-win-crt-environment-l1-1-0.dll
    -a----         2019/7/8     16:35          20744 api-ms-win-crt-filesystem-l1-1-0.dll
    -a----         2019/7/8     16:35          19720 api-ms-win-crt-heap-l1-1-0.dll
    -a----         2019/7/8     16:35          19208 api-ms-win-crt-locale-l1-1-0.dll
    -a----         2019/7/8     16:35          27912 api-ms-win-crt-math-l1-1-0.dll
    -a----         2019/7/8     16:35          19720 api-ms-win-crt-process-l1-1-0.dll
    -a----         2019/7/8     16:35          23304 api-ms-win-crt-runtime-l1-1-0.dll
    -a----         2019/7/8     16:35          24840 api-ms-win-crt-stdio-l1-1-0.dll
    -a----         2019/7/8     16:35          24840 api-ms-win-crt-string-l1-1-0.dll
    -a----         2019/7/8     16:35          21256 api-ms-win-crt-time-l1-1-0.dll
    -a----         2019/7/8     16:35          19208 api-ms-win-crt-utility-l1-1-0.dll
    -a----         2019/7/9     15:01         779541 base_library.zip
    -a----         2019/7/9     15:04            306 config.json
    -a----         2019/7/8     16:56        3205632 libcrypto-1_1-x64.dll
    -a----         2019/7/8     16:56        3573880 libGLESv2.dll
    -a----         2019/7/8     16:56         681472 libssl-1_1-x64.dll
    -a----         2019/7/9     15:04        1523306 main.exe
    -a----         2019/7/9     15:04           1029 main.exe.manifest
    -a----         2019/7/9     10:07         625808 MSVCP140.dll
    -a----         2019/7/8     16:35         193024 pyexpat.pyd
    -a----         2019/7/8     16:56          51712 python3.dll
    -a----         2019/7/8     16:35        3745280 python37.dll
    -a----         2019/7/8     16:56        6016632 Qt5Core.dll
    -a----         2019/7/8     16:56         440952 Qt5DBus.dll
    -a----         2019/7/8     16:56        6443640 Qt5Gui.dll
    -a----         2019/7/8     16:56        1338488 Qt5Network.dll
    -a----         2019/7/8     16:56        4010616 Qt5Qml.dll
    -a----         2019/7/8     16:56        3814520 Qt5Quick.dll
    -a----         2019/7/8     16:56         334968 Qt5Svg.dll
    -a----         2019/7/8     16:56         156280 Qt5WebSockets.dll
    -a----         2019/7/8     16:56        5574776 Qt5Widgets.dll
    -a----         2019/7/9     15:04            209 run.log
    -a----         2019/7/8     16:35          19456 select.pyd
    -a----         2019/7/8     16:56         108544 sip.pyd
    -a----         2019/7/8     16:35        1016584 ucrtbase.dll
    -a----         2019/7/8     16:35        1065472 unicodedata.pyd
    -a----         2019/7/8     16:35          87200 VCRUNTIME140.dll
    -a----         2019/7/8     16:35          81920 _bz2.pyd
    -a----         2019/7/8     16:35         125952 _ctypes.pyd
    -a----         2019/7/8     16:35          31744 _hashlib.pyd
    -a----         2019/7/8     16:35         249856 _lzma.pyd
    -a----         2019/7/8     16:35          68608 _socket.pyd
    -a----         2019/7/8     16:35         114176 _ssl.pyd

Loaded dlls when run in conda env:

    Process: main.exe Pid: 864  

    Name    Path
    kernel32.dll.mui    C:\Program Files\WindowsApps\Microsoft.LanguageExperiencePackzh-CN_18362.8.25.0_neutral__8wekyb3d8bbwe\Windows\System32\zh-CN\kernel32.dll.mui
    KernelBase.dll.mui  C:\Program Files\WindowsApps\Microsoft.LanguageExperiencePackzh-CN_18362.8.25.0_neutral__8wekyb3d8bbwe\Windows\System32\zh-CN\KernelBase.dll.mui
    user32.dll.mui  C:\Program Files\WindowsApps\Microsoft.LanguageExperiencePackzh-CN_18362.8.25.0_neutral__8wekyb3d8bbwe\Windows\System32\zh-CN\user32.dll.mui
    _ctypes.pyd C:\Users\xxx\Idp\dist\main\_ctypes.pyd
    _hashlib.pyd    C:\Users\xxx\Idp\dist\main\_hashlib.pyd
    _socket.pyd C:\Users\xxx\Idp\dist\main\_socket.pyd
    _ssl.pyd    C:\Users\xxx\Idp\dist\main\_ssl.pyd
    _AES.cp37-win_amd64.pyd C:\Users\xxx\Idp\dist\main\Crypto\Cipher\_AES.cp37-win_amd64.pyd
    libcrypto-1_1-x64.dll   C:\Users\xxx\Idp\dist\main\libcrypto-1_1-x64.dll
    libssl-1_1-x64.dll  C:\Users\xxx\Idp\dist\main\libssl-1_1-x64.dll
    main.exe    C:\Users\xxx\Idp\dist\main\main.exe
    MSVCP140.dll    C:\Users\xxx\Idp\dist\main\MSVCP140.dll
    qgif.dll    C:\Users\xxx\Idp\dist\main\PyQt5\Qt\plugins\imageformats\qgif.dll
    qicns.dll   C:\Users\xxx\Idp\dist\main\PyQt5\Qt\plugins\imageformats\qicns.dll
    qico.dll    C:\Users\xxx\Idp\dist\main\PyQt5\Qt\plugins\imageformats\qico.dll
    qjpeg.dll   C:\Users\xxx\Idp\dist\main\PyQt5\Qt\plugins\imageformats\qjpeg.dll
    qsvg.dll    C:\Users\xxx\Idp\dist\main\PyQt5\Qt\plugins\imageformats\qsvg.dll
    qtga.dll    C:\Users\xxx\Idp\dist\main\PyQt5\Qt\plugins\imageformats\qtga.dll
    qtiff.dll   C:\Users\xxx\Idp\dist\main\PyQt5\Qt\plugins\imageformats\qtiff.dll
    qwbmp.dll   C:\Users\xxx\Idp\dist\main\PyQt5\Qt\plugins\imageformats\qwbmp.dll
    qwebp.dll   C:\Users\xxx\Idp\dist\main\PyQt5\Qt\plugins\imageformats\qwebp.dll
    qwindows.dll    C:\Users\xxx\Idp\dist\main\PyQt5\Qt\plugins\platforms\qwindows.dll
    qwindowsvistastyle.dll  C:\Users\xxx\Idp\dist\main\PyQt5\Qt\plugins\styles\qwindowsvistastyle.dll
    QtCore.pyd  C:\Users\xxx\Idp\dist\main\PyQt5\QtCore.pyd
    QtGui.pyd   C:\Users\xxx\Idp\dist\main\PyQt5\QtGui.pyd
    QtSvg.pyd   C:\Users\xxx\Idp\dist\main\PyQt5\QtSvg.pyd
    QtWidgets.pyd   C:\Users\xxx\Idp\dist\main\PyQt5\QtWidgets.pyd
    sip.pyd C:\Users\xxx\Idp\dist\main\PyQt5\sip.pyd
    python3.dll C:\Users\xxx\Idp\dist\main\python3.dll
    python37.dll    C:\Users\xxx\Idp\dist\main\python37.dll
    Qt5Core.dll C:\Users\xxx\Idp\dist\main\Qt5Core.dll
    Qt5Gui.dll  C:\Users\xxx\Idp\dist\main\Qt5Gui.dll
    Qt5Svg.dll  C:\Users\xxx\Idp\dist\main\Qt5Svg.dll
    Qt5Widgets.dll  C:\Users\xxx\Idp\dist\main\Qt5Widgets.dll
    select.pyd  C:\Users\xxx\Idp\dist\main\select.pyd
    VCRUNTIME140.dll    C:\Users\xxx\Idp\dist\main\VCRUNTIME140.dll
    SortDefault.nls C:\Windows\Globalization\Sorting\SortDefault.nls
    ~FontCache-FontFace.dat C:\Windows\ServiceProfiles\LocalService\AppData\Local\FontCache\~FontCache-FontFace.dat
    advapi32.dll    C:\Windows\System32\advapi32.dll
    bcrypt.dll  C:\Windows\System32\bcrypt.dll
    bcryptprimitives.dll    C:\Windows\System32\bcryptprimitives.dll
    C_1252.NLS  C:\Windows\System32\C_1252.NLS
    cfgmgr32.dll    C:\Windows\System32\cfgmgr32.dll
    clbcatq.dll C:\Windows\System32\clbcatq.dll
    combase.dll C:\Windows\System32\combase.dll
    CoreMessaging.dll   C:\Windows\System32\CoreMessaging.dll
    CoreUIComponents.dll    C:\Windows\System32\CoreUIComponents.dll
    crypt32.dll C:\Windows\System32\crypt32.dll
    cryptbase.dll   C:\Windows\System32\cryptbase.dll
    cryptsp.dll C:\Windows\System32\cryptsp.dll
    d3d11.dll   C:\Windows\System32\d3d11.dll
    d3d9.dll    C:\Windows\System32\d3d9.dll
    DataExchange.dll    C:\Windows\System32\DataExchange.dll
    dcomp.dll   C:\Windows\System32\dcomp.dll
    igd9dxva64.dll  C:\Windows\System32\DriverStore\FileRepository\iigd_dch_base.inf_amd64_b95c9a044993331b\igd9dxva64.dll
    dwmapi.dll  C:\Windows\System32\dwmapi.dll
    DWrite.dll  C:\Windows\System32\DWrite.dll
    DXCore.dll  C:\Windows\System32\DXCore.dll
    dxgi.dll    C:\Windows\System32\dxgi.dll
    gdi32.dll   C:\Windows\System32\gdi32.dll
    gdi32full.dll   C:\Windows\System32\gdi32full.dll
    iertutil.dll    C:\Windows\System32\iertutil.dll
    imm32.dll   C:\Windows\System32\imm32.dll
    kernel.appcore.dll  C:\Windows\System32\kernel.appcore.dll
    kernel32.dll    C:\Windows\System32\kernel32.dll
    KernelBase.dll  C:\Windows\System32\KernelBase.dll
    locale.nls  C:\Windows\System32\locale.nls
    mpr.dll C:\Windows\System32\mpr.dll
    msasn1.dll  C:\Windows\System32\msasn1.dll
    msctf.dll   C:\Windows\System32\msctf.dll
    msvcp_win.dll   C:\Windows\System32\msvcp_win.dll
    msvcrt.dll  C:\Windows\System32\msvcrt.dll
    netapi32.dll    C:\Windows\System32\netapi32.dll
    netutils.dll    C:\Windows\System32\netutils.dll
    ntdll.dll   C:\Windows\System32\ntdll.dll
    ntmarta.dll C:\Windows\System32\ntmarta.dll
    ole32.dll   C:\Windows\System32\ole32.dll
    oleaut32.dll    C:\Windows\System32\oleaut32.dll
    powrprof.dll    C:\Windows\System32\powrprof.dll
    profapi.dll C:\Windows\System32\profapi.dll
    rmclient.dll    C:\Windows\System32\rmclient.dll
    rpcrt4.dll  C:\Windows\System32\rpcrt4.dll
    rsaenh.dll  C:\Windows\System32\rsaenh.dll
    sechost.dll C:\Windows\System32\sechost.dll
    SHCore.dll  C:\Windows\System32\SHCore.dll
    shell32.dll C:\Windows\System32\shell32.dll
    shlwapi.dll C:\Windows\System32\shlwapi.dll
    srvcli.dll  C:\Windows\System32\srvcli.dll
    TextInputFramework.dll  C:\Windows\System32\TextInputFramework.dll
    twinapi.appcore.dll C:\Windows\System32\twinapi.appcore.dll
    ucrtbase.dll    C:\Windows\System32\ucrtbase.dll
    umpdc.dll   C:\Windows\System32\umpdc.dll
    user32.dll  C:\Windows\System32\user32.dll
    userenv.dll C:\Windows\System32\userenv.dll
    uxtheme.dll C:\Windows\System32\uxtheme.dll
    version.dll C:\Windows\System32\version.dll
    win32u.dll  C:\Windows\System32\win32u.dll
    windows.storage.dll C:\Windows\System32\windows.storage.dll
    winmm.dll   C:\Windows\System32\winmm.dll
    winmmbase.dll   C:\Windows\System32\winmmbase.dll
    WinTypes.dll    C:\Windows\System32\WinTypes.dll
    ws2_32.dll  C:\Windows\System32\ws2_32.dll
    wtsapi32.dll    C:\Windows\System32\wtsapi32.dll    
like image 416
Sofair R. Avatar asked Jul 09 '19 09:07

Sofair R.


3 Answers

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()

like image 90
Sofair R. Avatar answered Oct 16 '22 14:10

Sofair R.


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.

like image 22
AggroCrag Avatar answered Oct 16 '22 14:10

AggroCrag


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')],
like image 21
dugwin Avatar answered Oct 16 '22 13:10

dugwin