pyinstaller 打包包含torch,torchvision,pyzbar 不成功解决方案

工具配置说明 同时被 2 个专栏收录
10 篇文章 0 订阅
8 篇文章 0 订阅

1 pyinstaller 对与torchvision 只能支持0.3.0以下,好像是这样,不然会出问题,

我用的版本是torch1.5.1 cpu  和torchvision0.6.1cpu,打包出现比如这样的错误

Traceback (most recent call last):
  File "Frame.py", line 7, in <module>
    from detect import detect_main
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "c:\users\administrator\anaconda3\envs\yolov3\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 623, in exec_module
    exec(bytecode, module.__dict__)
  File "detect.py", line 5, in <module>
    from models import *  # set ONNX_EXPORT in models.py
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "c:\users\administrator\anaconda3\envs\yolov3\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 623, in exec_module
    exec(bytecode, module.__dict__)
  File "models.py", line 5, in <module>
    from layers import *
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "c:\users\administrator\anaconda3\envs\yolov3\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 623, in exec_module
    exec(bytecode, module.__dict__)
  File "layers.py", line 4, in <module>
    from utils import *
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "c:\users\administrator\anaconda3\envs\yolov3\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 623, in exec_module
    exec(bytecode, module.__dict__)
  File "utils.py", line 19, in <module>
    import torchvision1
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "c:\users\administrator\anaconda3\envs\yolov3\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 623, in exec_module
    exec(bytecode, module.__dict__)
  File "torchvision1\__init__.py", line 3, in <module>
    from torchvision1 import models
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "c:\users\administrator\anaconda3\envs\yolov3\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 623, in exec_module
    exec(bytecode, module.__dict__)
  File "torchvision1\models\__init__.py", line 12, in <module>
    from . import detection
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "c:\users\administrator\anaconda3\envs\yolov3\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 623, in exec_module
    exec(bytecode, module.__dict__)
  File "torchvision1\models\detection\__init__.py", line 1, in <module>
    from .faster_rcnn import *
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "c:\users\administrator\anaconda3\envs\yolov3\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 623, in exec_module
    exec(bytecode, module.__dict__)
  File "torchvision1\models\detection\faster_rcnn.py", line 7, in <module>
    from torchvision1.ops import misc as misc_nn_ops
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "c:\users\administrator\anaconda3\envs\yolov3\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 623, in exec_module
    exec(bytecode, module.__dict__)
  File "torchvision1\ops\__init__.py", line 8, in <module>
    from .poolers import MultiScaleRoIAlign
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "c:\users\administrator\anaconda3\envs\yolov3\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 623, in exec_module
    exec(bytecode, module.__dict__)
  File "torchvision1\ops\poolers.py", line 41, in <module>
    class LevelMapper(object):
  File "torch\jit\__init__.py", line 1280, in script
    _compile_and_register_class(obj, _rcb, qualified_name)
  File "torch\jit\__init__.py", line 1107, in _compile_and_register_class
    ast = get_jit_class_def(obj, obj.__name__)
  File "torch\jit\frontend.py", line 154, in get_jit_class_def
    sourcelines, file_lineno, filename = get_source_lines_and_file(cls, torch._C.ErrorReport.call_stack())
  File "torch\_utils_internal.py", line 53, in get_source_lines_and_file
    raise OSError(msg)
OSError: Can't get source for <class 'torchvision1.ops.poolers.LevelMapper'>. TorchScript requires source access in order to carry out compilatio
n, make sure original .py files are available. Original error: could not get source code
[13176] Failed to execute script Frame

尝试了很多办法,都不行。我想不如直接把torch和torchvision当成自己项目的一部分算了。

然后我直接把torch和torchvision这两个包从安装路径粘出来放到项目根目录下,作为项目自己的导入包。

然后从开发环境中卸载者两个包,既pip uninstall torch, 和pip uninstall torchvision。

卸载完之后,我把项目中的torchvision重命名为torchvision1。如下图,然后在项目中需要导入torchvison的地方,全部改成torchvision1,因为要导入项目中的torchvision1,还有就是为了和环境中的torchvision作区分嘛。这个改名字是为了保险免得和环境中的搞混了。

然后用pyinstaller打包,pyinstaller -D -c xxx.py。    -D是打包成文件夹,-c是运行时有控制台的输出信息。具体用法自己百度。

完成后,可能有些包不会打进去,需要自己把这些包从安装目录中粘过去。

比如打包后,torvhvision1这个包不会自己添加进去,手动放过去,还有其他的需要的包。运行的时候看看,粘过去就好。

差不多就完成了。

运行的时候最好最好在生成的打包文件夹下运行,特别cmd运行的时候,一定要先打包的文件夹。不然有可能出问题。

 

还有就时如果用到cv2 还有numpy,注意这两个包的版本应该匹配。不然会报错

比如

ImportError: numpy.core.multiarray failed to import

解决方案:

解决思路,改变控制台的编码格式为utf-8
解决方法01,在控制台中输入 chcp 65001,然后再使用pyinstaller -D  xxx.py 打包成功。  这个试一下,可能有用。

第二个是我的解决方案。

这是因为cv2要求的numpy版本与你装的numpy版本不一样,导致冲突;网上很多说升级numpy,但你把numpy升的再新,cv2要求的版本却是旧的好吗?

所以将两者都重新安装为适配版本就行,如下:

1

pip install opencv-python==3.4.2.17 numpy==1.14.5

或者可以试试只将numpy版本降低

1

pip install numpy==1.14.5

差不多就这样。

结果如图

运行结果

  • 3
    点赞
  • 0
    评论
  • 5
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值