当前位置:网站首页>Pyuninstaller package exe cannot find the source code when running, function error oserror: could not get source code

Pyuninstaller package exe cannot find the source code when running, function error oserror: could not get source code

2022-04-23 21:02:00 Top of the program

Use today pyinstaller take Detectron2 A file of is packaged into exe, However, the following error occurred during the operation of the program

Traceback (most recent call last):
  File "torch\_sources.py", line 21, in get_source_lines_and_file
    sourcelines, file_lineno = inspect.getsourcelines(obj)
  File "inspect.py", line 956, in getsourcelines
  File "inspect.py", line 787, in findsource
OSError: could not get source code

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "cell_analyse.py", line 8, in <module>
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "PyInstaller\loader\pyimod03_importers.py", line 495, in exec_module
  File "detectron2\model_zoo\__init__.py", line 8, in <module>
    from .model_zoo import get, get_config_file, get_checkpoint_url, get_config
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "PyInstaller\loader\pyimod03_importers.py", line 495, in exec_module
  File "detectron2\model_zoo\model_zoo.py", line 9, in <module>
    from detectron2.modeling import build_model
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "PyInstaller\loader\pyimod03_importers.py", line 495, in exec_module
  File "detectron2\modeling\__init__.py", line 2, in <module>
    from detectron2.layers import ShapeSpec
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "PyInstaller\loader\pyimod03_importers.py", line 495, in exec_module
  File "detectron2\layers\__init__.py", line 2, in <module>
    from .batch_norm import FrozenBatchNorm2d, get_norm, NaiveSyncBatchNorm, CycleBatchNormList
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "PyInstaller\loader\pyimod03_importers.py", line 495, in exec_module
  File "detectron2\layers\batch_norm.py", line 4, in <module>
    from fvcore.nn.distributed import differentiable_all_reduce
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "PyInstaller\loader\pyimod03_importers.py", line 495, in exec_module
  File "fvcore\nn\__init__.py", line 4, in <module>
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "PyInstaller\loader\pyimod03_importers.py", line 495, in exec_module
  File "fvcore\nn\focal_loss.py", line 52, in <module>
  File "torch\jit\_script.py", line 1307, in script
    ast = get_jit_def(obj, obj.__name__)
  File "torch\jit\frontend.py", line 233, in get_jit_def
    parsed_def = parse_def(fn)
  File "torch\_sources.py", line 95, in parse_def
    sourcelines, file_lineno, filename = get_source_lines_and_file(fn, ErrorReport.call_stack())
  File "torch\_sources.py", line 28, in get_source_lines_and_file
    raise OSError(msg) from e
OSError: Can't get source for <function sigmoid_focal_loss at 0x000002038751F9D8>. TorchScript requires source access in order to carry out compilation, make sure original .py files are available.
[75064] Failed to execute script 'cell_analyse' due to unhandled exception!

This error is directly prompted as sigmoid_focal_loss The source code of this function is not found , The program to add source code is File “inspect.py”, line 787, in findsource, Further tracking code discovery can be done through getfile This function can find the specific modules to be added to the program , I added the following line to the code of this function , Print out which module has not been added .

def getfile(object):
    """Work out which source or compiled file an object was defined in."""
    if ismodule(object):
        if getattr(object, '__file__', None):
            return object.__file__
        raise TypeError('{!r} is a built-in module'.format(object))
    if isclass(object):
        if hasattr(object, '__module__'):
            object = sys.modules.get(object.__module__)
            if getattr(object, '__file__', None):
                return object.__file__
        raise TypeError('{!r} is a built-in class'.format(object))
    if ismethod(object):
        object = object.__func__
    if isfunction(object):
        object = object.__code__
        **print(object)**     **# Add this line of code here , Print out which source code is not added successfully **
    if istraceback(object):
        object = object.tb_frame
    if isframe(object):
        object = object.f_code
    if iscode(object):
        return object.co_filename
    raise TypeError('module, class, method, function, traceback, frame, or '
                    'code object was expected, got {}'.format(
                    type(object).__name__))

Personally, I feel that this pit father's error is related to the loading of source code ,Detectron2 Used inspect This module , Load source code , Lead to Pyinstaller When analyzing File Dependencies , Did not add the relevant source code .
After adding the print code , Compile again to generate exe, Rerun , The results are as follows log

<code object norm at 0x0000020384365780, file "torch\functional.py", line 1309>
<code object sigmoid_focal_loss at 0x000002038750B390, file "fvcore\nn\focal_loss.py", line 7>
<code object sigmoid_focal_loss at 0x000002038750B390, file "fvcore\nn\focal_loss.py", line 7>
Traceback (most recent call last):
  File "torch\_sources.py", line 21, in get_source_lines_and_file
    sourcelines, file_lineno = inspect.getsourcelines(obj)
  File "inspect.py", line 956, in getsourcelines
  File "inspect.py", line 787, in findsource
OSError: could not get source code

So we can see that , yes fvcore This module has not been added to the feasible exe Under the table of contents . When I put... Under the source code base fvcore Copy to build exe After the directory , Run again , It's not in the newspaper fvcore Error not found , You can report the error that the source code of other modules cannot be found , I copied it , The program can run smoothly .

Of course, the formal solution should be through modification .spec file , Automatically add the dependent files to the compiled files when packaging , This step , You can refer to pyinstaller The official resolvent

版权声明
本文为[Top of the program]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/111/202204210545089993.html