0


Pyinstaller打包OSError: could not get source code【终极解决】

pyinstaller 打包的时候,发现只要是

@torch.jit.script

装饰的函数,会报以下错误:

Traceback (most recent call last):
  File "torch/_sources.py", line 25, in get_source_lines_and_file
  File "inspect.py", line 1123, in getsourcelines
  File "inspect.py", line 960, in findsource
OSError: could not get source code
......
OSError: Can't get source for <function network.split_utils at 0x7f90b842f5b0>
<function network.split_utils at 0x7f90b842f5b0>

中的

split_utils

是你的函数名。找到这个函数所在的py文件,并按照下面的方法添加进打包datas!
参考:https://github.com/pyinstaller/pyinstaller/issues/6865
将包含

@torch.jit.script

.py

文件打包进可执行文件即可。eg:

ROOT_DIR="path/to/your/project/folder/"
a = Analysis(
    ['main.py'],
    pathex=[ROOT_DIR,],
    binaries=[],
    datas=[
    (ROOT_DIR+"your_module/utils.py", "your_module/")   # 包含原始py文件
    ]

重点是:

 (ROOT_DIR+"your_module/utils.py", "your_module/")

如果你担心源码泄漏,请更换其他方式。比如放弃使用加速,也就是不使用

@torch.jit.script

毕!


本文转载自: https://blog.csdn.net/yuantao18800/article/details/140844915
版权归原作者 苑先森 所有, 如有侵权,请联系我们删除。

“Pyinstaller打包OSError: could not get source code【终极解决】”的评论:

还没有评论