解决在使用pyinstaller打包pytorch脚本的情况下遇到could not get source code的问题

解决在使用pyinstaller打包pytorch脚本的情况下遇到could not get source code的问题

这个问题折磨了我两天,终于找到了解决的办法,啊!

遇到的问题如下:

Traceback (most recent call last):
  File "site-packages\torch\_utils_internal.py", line 46, in get_source_lines_and_file
  File "inspect.py", line 936, in getsourcelines
  File "inspect.py", line 767, in findsource
OSError: could not get source code

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "prediction.py", line 4, in <module>
    import torch
  File "", line 968, in _find_and_load
  File "", line 957, in _find_and_load_unlocked
  File "", line 673, in _load_unlocked
  File "C:\ProgramData\Anaconda3\envs\pytorch\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 623, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packages\torch\__init__.py", line 367, in <module>
  File "", line 968, in _find_and_load
  File "", line 957, in _find_and_load_unlocked
  File "", line 673, in _load_unlocked
  File "C:\ProgramData\Anaconda3\envs\pytorch\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 623, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packages\torch\distributions\__init__.py", line 112, in <module>
  File "", line 968, in _find_and_load
  File "", line 957, in _find_and_load_unlocked
  File "", line 673, in _load_unlocked
  File "C:\ProgramData\Anaconda3\envs\pytorch\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 623, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packages\torch\distributions\von_mises.py", line 55, in <module>
  File "site-packages\torch\jit\__init__.py", line 1287, in script
  File "site-packages\torch\jit\frontend.py", line 164, in get_jit_def
  File "site-packages\torch\_utils_internal.py", line 54, in get_source_lines_and_file
OSError: Can't get source for <function _rejection_sample at 0x00000212A1902400>. TorchScript requires source access in order to carry out compilation, make sure original .py files are available. Original error: could not get source code
[44632] Failed to execute script extractor

解决在使用pyinstaller打包pytorch脚本的情况下遇到could not get source code的问题_第1张图片

看到网上说可能是torchvision版本的问题,重装torchvision,无效。

网上见到最多的一个回答就是:

”I have also been running into this issue, have not found a solution“

最后找到了解决问题的办法:
在.py文件夹下找到生成的.spec文件,用文本的方式打开它,并做如下更改:

block_cipher = None
excluded_modules = ['torch.distributions'] # 加入这一行

a = Analysis(['xxx.py'],
             pathex=['path'],
             binaries=[],
             datas=[],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=excluded_modules, # 把=None改成=excluded_modules
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)

其他部分不用更改。

之后再运行:

pyinstaller -F xxx.spec

之后就可以完美运行pytorch脚本啦,并且发现不需要安装torch也可以运行。

之后再运行:

pyinstaller -F xxx.spec

之后就可以完美运行pytorch脚本啦,并且发现不需要安装torch也可以运行。

同时,如果在需要打包的.py文件中,需要import 自己所写的包,一个非常简便(吗?)的方法就是,在python的site-packages文件夹里把那个.py文件复制过去,之后在.spec中 的hiddenimports = []里加入这个文件,如'mymodel'(包括引号),再重新编译spec文件就可以啦。

你可能感兴趣的:(python,pytorch)