作为一名初学PyQt5的萌新,emmmm
好吧,我承认,我连基础都没看完,就直接去写代码了。
ps :如果是大佬可以点击解决方案查看问题的处理方法。
前言
目录
之前写了一个爬取某站图片的python小程序,闲来无事,想要改成图形界面来展示,用pydesigner画完界面,写好代码,在花了一个周末的时间调好了整个程序后,愉快的分享给了小伙伴,然后得到了以下回复。
我靠,有没有搞错,我自己跑的好好的,不过鉴于pyqt5写界面和代码的时候已经踩了无数个坑,多这一个不算什么。
于以重新以不关闭窗口打包,看看到底报了啥错
pyinstaller -F main_search_ui.py -n ****
进入到打包好的 exe 文件的路径下打开命令行,执行打包的exe
cmd窗口下输入命令 .\****.exe
emmm,不报错,于是想了一下可能的原因,无非就是库的问题。
问题分析
那么分析一下,我之前打的包别人直接可以用,那么就是pyqt的库的问题。如此,这些文件肯定在某个环境变量的路径下面。我安装了pycharm,用的是anaconda的库管理,于是找到python相关环境变量的路径,依次改了python的安装文件夹名(本来是python37)改为了37.1,anaconda的安装路径的文件夹名,以及pycharm生成的项目包的文件夹名。最终确定为anaconda 的环境变量路径问题。
问题是找到了,果然,如果没有安装PyQt的库,那么就会报 unable to find Qt5Core.dll on PATH的错误
然后尝试各种方法:
打包时限定pyqt的路径 pyinstaller -F --paths C:/****/PyQt5/Qt/bin main_search_ui.py 嗯,没用。
限定打包时pyinstaller的路径为项目中的路径,也没用。
继续百度 unable to find Qt5Core.dll on PATH 。。。。。。。。。。。。。
还好有其他的大佬也遇到了同样的问题:https://blog.csdn.net/zwyact/article/details/99778898
嗯,明白是库的问题就难办了,就只有这样放弃了?
问题深入
当然不,既然是库的问题,那就改库好了。
找到这个文件,看看怎么写的再说。
pycharm项目生成的lib下有,如果项目下没有找到venv\lib\site-packages\PyQt5,自行百度找包的方法。
附上库文件源码:
# 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") 抛出来的异常
解决方案
init时要通过配置好的环境变量来加载PyQt5的文件,那么换个思路,我把pyqt包拷到当前目录下,并且修改上面代码,读取当前路径下的PyQt包即可。
dll_dir = 'PyQt5\\Qt\\bin'
将venv\lib\site-packages的PyQt5文件夹整个复制出来,放到和打包好的exe文件同目录下,运行即可。
结语
此方法可以解决exe打不开的问题,但是PyQt5的包有几十兆,占用空间还是不小的,只能希望官方尽快修复这个bug了。