【MBJC】Pyinstaller打包 yolov5 PyQt5 GUI程序掉坑Debug记录【003】

主要问题

ImportError: DLL load failed while importing QtCore: 找不到指定的程序。

ImportError: DLL load failed while importing QtGui: 找不到指定的程序。

ImportError: DLL load failed while importing QtWidgets: 找不到指定的程序。

ImportError: DLL load failed while importing QtWebEngineWidgets: 找不到指定的程序。

使用打包命令后

pyinstaller -D -i logo.ico main.py

打包前,在Anaconda配好的环境主程序可以正常运行使用;打包后,在当前目录下自动生成的dist文件夹中找到的main.exe,双击运行闪退,于是使用cmd,打开exe所在当前路径,使用debug命令

main.exe -d

查看报错为

ImportError: DLL load failed while importing QtCore: 找不到指定的程序。

报错位置代码为

from PyQt5.QtCore import *

博主Debug过程发现网上也有很多类似使用Pyinstaller打包含PyQT库报dll找不到的情况,尝试了很多方法依旧无法解决。最终还是由几篇blog启发,找到解决方法。其实与之类似的一些报错如

ImportError: DLL load failed while importing QtCore: 找不到指定的程序。
ImportError: DLL load failed while importing QtGui: 找不到指定的程序。
ImportError: DLL load failed while importing QtWidgets: 找不到指定的程序。
ImportError: DLL load failed while importing QtWebEngineWidgets: 找不到指定的程序。

报错位置都在导入模块,切换导入的写法好像并没用什么用

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtWebEngineWidgets import *

from PyQt5 import QtCore, QtGui, QtWidgets

都可以归结为Pyinstaller调取PyQt动态库失败,根本原因,在博主看来还是pyinstaller 和 PyQt的版本可能存在依赖匹配的关系,主要还是环境的问题。解决方法降低pyinstaller的版本,博主使用最笨的办法依次降低版本,从5.7到5.4都不行,5.3没试,最后降至5.2、5.1可行。
最后选择了pyinstaller 5.2,anaconda prompt中使用pip命令安装对应版本的库

pip install pyinstaller==5.2

原环境

  • 操作系统 Win 11
  • IDE Pycharm + Anaconda
  • python 3.7.13
  • pyinstaller 5.7.0
  • pyqt5 5.15.9

修改后环境

由于之前找不到解决方法,重新配置了环境,和python版本应该没关系。

  • python 3.8.16
  • pyinstaller 5.2
  • pyqt5 5.15.9

ImportError: ERROR: recursion is detected during loading of “cv2” binary extensions. Check OpenCV installation.

再次debug后,谢天谢地,有了变化,报错改变,我们的老朋友opencv请求加入战斗,它说找不到dll,可我核对了路径Anaconda/envs/“你的环境名称”/Lib/site-package/中的opencv-python发现和打包后dist文件夹中的一模一样,估计又是pyinstaller调取动态库的锅,最后也是找到了解决方法,降低opencv-python的版本至4.5.3.56。

pip install opencv-python=4.5.3.56

原环境

  • 操作系统 Win 11
  • IDE Pycharm + Anaconda
  • python 3.7.13
  • pyinstaller 5.7.0
  • pyqt5 5.15.9
  • opencv-python 4.6.0.66

修改后环境

由于之前找不到解决方法,重新配置了环境,和python版本应该没关系。

  • python 3.8.16
  • pyinstaller 5.2
  • pyqt5 5.15.9
  • opencv-python4.5.3.56

FileNotFoundError: [Errno 2] No such file or directory: ‘logs/ep125-loss0.073-val_loss0.054.pth’

再次debug后,报错有了变化,此时博主内心是平静的,因为离胜利已经很接近了。这类问题通常是因为一些代码需要读取txt文件里面的数据,模型等配置文件,代码可能是用相对路径调用的,exe运行在当前目录下找不到,就会报错。只需要把原文件夹目录下的模型,配置文件,全部放进自动生成的dist文件夹中的main文件夹即可。如果出现图片等资源,html网页等资源加载不出来也大概是这个问题,只需要把用到的css、html、js一起扔进去即可。
选中的是我扔进去的一些资源文件。
在这里插入图片描述
成功运行!
在这里插入图片描述

参考blog

链接: 【已解决】pyinstaller打包后出现ImportError: DLL load failed while importing QtWebEngineWidgets: 找不到指定的程序。

链接: ImportError: ERROR: recursion is detected during loading of “cv2“ binary extensions. Check OpenCV in

完结撒花

第一次用Pyintaller打包PyQt yolo的GUI程序,也是掉了不少坑,痛苦面具。幸运的是看到了一些blog为我指明方向,在不断尝试摸索后成功了,特此记录总结一下。等待、遇见和我一样正在debug的你。Good luck!完结撒花!

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/noneNull0/article/details/128950684