bootstrap项目pyinstaller打包包含torch,torchvision,pyzbar不成功解决⽅案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: ultiarray failed to import
解决⽅案:
解决思路,改变控制台的编码格式为utf-8
解决⽅法01,在控制台中输⼊ chcp 65001,然后再使⽤pyinstaller -D  xxx.py 打包成功。  这个试⼀下,可能有⽤。
第⼆个是我的解决⽅案。
这是因为cv2要求的numpy版本与你装的numpy版本不⼀样,导致冲突;⽹上很多说升级numpy,但你把numpy升的再新,cv2要求的版本却是旧的好吗?
所以将两者都重新安装为适配版本就⾏,如下:
1pip install opencv-python==3.4.2.17numpy==1.14.5
或者可以试试只将numpy版本降低
1pip install numpy==1.14.5
差不多就这样。
结果如图
运⾏结果

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。