pycharm、pyqt5、pyinstaller打包运行后出现错误ImportError: unable to find Qt5Core.dll on PATH的解决

pycharm、pyqt5、pyinstaller打包运行后出现错误ImportError: unable to find Qt5Core.dll on PATH的解决

在 PyQt5.13 中,其 __init__.py 文件内容如下:

# Copyright (c) 2019 Riverbank Computing Limited <info@riverbankcomputing.com>
#
# This file is part of PyQt5.
#
# This file may be used under the terms of the GNU General Public License
# version 3.0 as published by the Free Software Foundation and appearing in
# the file LICENSE included in the packaging of this file.  Please review the
# following information to ensure the GNU General Public License version 3.0
# requirements will be met: http://www.gnu.org/copyleft/gpl.html.
#
# If you do not wish to use this file under the terms of the GPL version 3.0
# then you may purchase a commercial license.  For more information contact
# info@riverbankcomputing.com.
#
# This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
# WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.


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

 

从上面可以看到,错误是在 raise ImportError("unable to find Qt5Core.dll on PATH") 这里报出来。这段代码的大致意思就是在系统的环境变量 PATH 路径下去找 Qt5Core.dll 文件,而实际上这个文件并不在 PATH 路径下。

我们在使用 PyInstaller 打包时,指定 -F 选项理论上是会将 Qt5Core.dll 文件打包进可执行文件的,但是由于 PyQT5.13 默认在系统 PATH 路径下查找,因此出错了。

我们可以将下面的内容注释掉,然后一切运行正常。

else:
raise ImportError("unable to find Qt5Core.dll on PATH")
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值