我正在尝试将包含PyTorch的脚本转换为Windows可执行文件。我的脚本导入如下:
import numpy.core.multiarray # which is a workaround for "ImportError: numpy.core.multiarray failed to import"
import six # which is workaround for "ModuleNotFoundError: No module named 'six'"
import torch
import torch.nn as nn
import warnings
import argparse
import json
import math
import numpy as np
import jsonschema
import os
from datetime import datetime
from sklearn.mixture import GaussianMixture
from scipy.io import wavfile
from scipy.signal import get_window
from scipy.signal import spectrogram
我在使用命令:
pyinstaller --hidden-import pkg_resources.py2_warn extractor.py
PyInstaller在创建. exe时不会抛出错误,但当我运行. exe时,我得到:
Traceback (most recent call last):
File "site-packages\torch\_utils_internal.py", line 46, in get_source_lines_and_file
File "inspect.py", line 967, in getsourcelines
File "inspect.py", line 798, in findsource
OSError: could not get source code
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "extractor.py", line 3, in <module>
import torch
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "C:\ProgramData\Anaconda3\envs\forexe2\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 "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "C:\ProgramData\Anaconda3\envs\forexe2\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 "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "C:\ProgramData\Anaconda3\envs\forexe2\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 53, in get_source_lines_and_file
OSError: Can't get source for <function _rejection_sample at 0x0000000006892F70>. TorchScript requires source access in order to carry out compilation, make sure original .py files are available. Original error: could not get source code
[5704] Failed to execute script extractor
我不明白。这可能是类似的问题this question。是什么导致的问题?
我使用的是conda env,通过pip安装torch(这是一个正确连接torch的变通方案)。
- windows 10
- Python 3.8.2语言
- 焊炬1.5.0 + Cu101
- torchvision 0.6.0 + cu101(也尝试使用0.2.2)
- PyInstaller 3.6版
2条答案
按热度按时间uhry853o1#
Torch是开源的,所以你可以在Torch GitHub上搜索
_rejection_sample
函数,这样就可以识别出问题文件是torch.distributions.von_mises,如果你的程序没有使用torch.distributions
模块,你可以简单地通过修改pyinstaller生成的.spec
文件来排除它。您应该只需要在上面指定的两个位置进行更改。其余位置应该已经存在。
然后,build from the .spec file使用
以后,请考虑在生成项目时使用调试标志。这也可以标识导致问题的文件的位置。
3lxsmp7m2#
除了给出的答案,如果它仍然不工作下面的解决方案为我工作.
只是在导入torch上加一点代码,我报错的模块是main文件,包也是main文件,所以我直接把代码加到main的导入torch上,我其他模块也用导入torch,但不用这么做