【pyinstaller打包问题汇总】

1.pyinstaller

报错1:

Traceback (most recent call last):
  File "test_plate.py", line 31, in <module>
    Plate = detect_plate()
  File "package.py", line 41, in __init__
    self.model = torch.load('./weights/last.pt')["model"].float().eval()  # load FP32 model
  File "torch\serialization.py", line 608, in load
  File "torch\serialization.py", line 787, in _legacy_load
ModuleNotFoundError: No module named 'models.yolo'
[21064] Failed to execute script 'test_plate' due to unhandled exception!  

解决:pyinstaller -F test_plate.py --hidden-import models.yolo

报错2:

FileNotFoundError: [WinError 2] 系统找不到指定的文件。: ‘C:\\Users\\AppData\\Roaming\\pyinstaller\\bincache00_py39_64bit\\ucrtbase.dll’

pyinstaller 提示FileNotFoundError: [WinError 2] 系统找不到指定的文件
上次使用pyinstaller就碰到 ucrtbase.dll 的问题,当时可能是因为权限或者杀毒软件的问题,把360退出然后在通过pyinstaller打包一次就解决问题,但是隔了一段时间后再次使用pyinstaller打包,又碰到了 ucrtbase.dll 的问题,这次提示的不少权限问题,而是 : FileNotFoundError: [WinError 2] 系统找不到指定的文件。: ‘C:\Users\AppData\Roaming\pyinstaller\bincache00_py39_64bit\ucrtbase.dll’
那根据提示,已知是找不到指定文件,然后去提示的这个C盘目录下去找 ucrtbase.dll 文件,发现相关目录是真的没有这个文件,然后通过搜索去解决问题,回答和解决方法都是花里胡哨,然后就想着去下载一个ucrtbase.dll 文件,终于在dll上找到了。https://cn.dll-files.com/ucrtbase.dll.html 需要哪个版本就下载哪个版本,下载后就把这个压缩包解压,然后把dll文件放进提示缺失的文件夹即可。

报错3:

FileNotFoundError: [WinError 3] 系统找不到指定的路径。: 'C:\\Users\\admin\\AppData\\Local\\Temp\\_MEI236042\\utils\\torch_utils.pyc'
可尝试方法:

1.错误原因可能是Pyinstaller没有连接到utils这个包里的文件,所以手动建立连接,再以下位置(根据自己位置修改)Anaconda3\envs\torch\Lib\site-packages\PyInstaller\hooks中添加名为hooks-utils.py的文件,内容如下:

from PyInstaller.utils.hooks import collect_data_files

datas = collect_data_files("utils")

这个方法我没成功,觉得应该是因为utils不是环境里的包,而且我出现问题的是utils\torch_utils.py,可能是应该添加torch_utils.py到路径里。
参考:https://www.freesion.com/article/87871463440/
https://blog.csdn.net/m0_46825740/article/details/120825545

2.上述方法重新打包后还是有错,所以仔细看了下exe执行的报错,发现问题是出在 t = datetime.datetime.fromtimestamp(Path(path).stat().st_mtime)这一句,我觉得可能是和他的Path有关系,好像之前在哪里看到过有路径比较容易有问题,然后看了下这段代码没什么用,就注释掉换别的写法,把import utils.torch_utils 注释了,重新打包

 File "frame_handle.py", line 38, in __init__
    # self.device = select_device('0')   # 选择运行所在设备
  File "utils\torch_utils.py", line 65, in select_device
    s = f'YOLOv5  {git_describe() or date_modified()} torch {torch.__version__} '  # string
  File "utils\torch_utils.py", line 50, in date_modified
    t = datetime.datetime.fromtimestamp(Path(path).stat().st_mtime)
  File "pathlib.py", line 1158, in stat
  File "pathlib.py", line 387, in wrapped
FileNotFoundError: [WinError 2] 系统找不到指定的文件。: 'C:\\Users\\admin\\AppData\\Local\\Temp\\_MEI220122\\utils\\torch_utils.pyc'
[26532] Failed to execute script 'version_detect' due to unhandled exception!

这次还真成了,耶,果然改bug还是得好好看报错

总结:我觉得包越多越容易出错,所以写代码的时候还是要注意精简

报错4:Check OpenCV installation.

ImportError: ERROR: recursion is detected during loading of "cv2" binary extensions. Check OpenCV installation.
[13264] Failed to execute script 'onmyoji' due to unhandled exception!

解决: 版本问题 pyinstaller:4.7, opencv-python:4.5.1.48

报错5:Most probably this dynlib/dll was not found when the application was frozen.

原因可能是打包时缺少动态库之类的dll,我这里是打包海康SDK时候的HCNetSDK.dll,解决方式如下:

hk的是在类中动态库加载失败
解决:修改spec文件,添加动态库路径,binaries=[('F:\...\Project-Merge1018\lib_HK\win', '.'),('F:\...\Project-Merge1018\Libs\win64', '.')]
# 补充: 大华SDK的是在环境包中的NetSDK/NetSDK.py中191行netsdkdllpath(动态库加载失败)——>47行netsdkdllpath_dict——>动态库加载失败

报错6:打包paddleocr遇到报错

FileNotFoundError: [WinError 2] 系统找不到指定的文件。:'C:\\Users\\admin\\AppData\\Local\\Temp\\_MEI290962\\paddle\fluid\…\libs’

你可能感兴趣的:(python)