0


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

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

  1. @torch.jit.script

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

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

中的

  1. split_utils

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

  1. @torch.jit.script

  1. .py

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

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

重点是:

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

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

  1. @torch.jit.script

毕!


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

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

还没有评论