且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何修复“ImportError:无法在 PATH 上找到 Qt5Core.dll"在 pyinstaller 捆绑了 python 应用程序之后

更新时间:2023-08-21 19:14:22

如题所述,在conda控制台启动捆绑的应用程序时,运行正常,所有加载的DLL,由ProcessExplorer导出code>,位于由 pyinstaller 创建的 dist 目录中.所以问题是包含 pyqt DLL 的路径不在系统的 PATH 环境中.也许这是pyinstaller的一个错误.解决方案是手动将程序路径添加到系统 PATH env.

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