打包成功,但运行exe时闪退,拉进终端调试一下发现以下错误
OSError: Can’t get source for <function Boxes3D.init at 0x000002032D1A4C80>. TorchScript requires source access in order to carry out compilation, make sure original .py files are available.
[5956] Failed to execute script ‘ListViewDemo_main’ due to unhandled exception!
将torch重装后问题解决
参考:https://www.codenong.com/cs106577121/
出现新问题!
运行后导入图片
调用分析函数时,出现问题:
no model named kornia
应该是kornia包未导入,那就导入呗
首先尝试标出路径
pyinstaller -p D:\CSDM\kdsb\venv\Lib\site-packages -p D:\CSDM\kdsb\venv\Lib\site-packages\PyQt6\Qt6\bin -p D:\CSDM\kdsb\venv\Lib\site-packages\kornia -F ListViewDemo_main.py
好的,直接打包失败
cannot import “open”
尝试另一种导入方法
pyinstaller -p D:\CSDM\kdsb\venv\Lib\site-packages -p D:\CSDM\kdsb\venv\Lib\site-packages\PyQt6\Qt6\bin -F ListViewDemo_main.py --hidden-import kornia
打包成功!
but调用函数时梅开二度(大漏特漏,错误提示不一样
重装torch,无用,尝试另一种解决方法
打开自动生成的xxxx.spec文件
excluded_modules = ['torch.distributions'] #加入
然后运行
pyinstaller -F xxxx.spec
无用,应该并非torch版本问题
注:观察错误是否一致,不一致不要盲目解决/(ㄒoㄒ)/
!!!在引用torch的上方代码最上方加入以下代码可解决:
def script_method(fn, _rcb=None):
return fn
def script(obj, optimize=True, _frames_up=0, _rcb=None):
return obj
import torch.jit
script_method1 = torch.jit.script_method
script1 = torch.jit.script
torch.jit.script_method = script_method
torch.jit.script = script
以上代码加在import torch上方就行,否则pyinstaller有可能不会将该文件识别为自定义函数,导致该文件不能自动打包,出现以下错误找不到cnn.mian
main.py是我自己写的库,在目录cnn下
-pyth
-cnn
-main.py
主函数.py
记录一下最后的打包代码
pyinstaller -p D:\CSDM\kdsb\venv\Lib\site-packages -p D:\CSDM\kdsb\venv\Lib\site-packages\PyQt6\Qt6\bin -p D:\CSDM\kdsb\cnn -F ListViewDemo_main.py --hidden-import kornia
感谢大佬!
https://www.codeleading.com/article/26186178330/
总结:
OSError: Can’t get source for <function Boxes3D.init at 0x000002032D1A4C80>. TorchScript requires source access in order to carry out compilation, make sure original .py files are available.
[5956] Failed to execute script ‘ListViewDemo_main’ due to unhandled exception!
重装torch
OSError: Can’t get source for <function Boxes3D.init at 0x000002A938B17A60>.TorchScript requires source access in order to carry out compilation, make sure original .py files are available.
在引用torch的上方加入以下代码:
def script_method(fn, _rcb=None):
return fn
def script(obj, optimize=True, _frames_up=0, _rcb=None):
return obj
import torch.jit
script_method1 = torch.jit.script_method
script1 = torch.jit.script
torch.jit.script_method = script_method
torch.jit.script = script