marcelotduarte / cx_Freeze

cx_Freeze creates standalone executables from Python scripts, with the same performance, is cross-platform and should work on any platform that Python itself works on.
https://marcelotduarte.github.io/cx_Freeze/
Other
1.33k stars 217 forks source link

TorchVision 0.17.1+cu118.: OSError: could not get source code #2280

Closed bw7715 closed 6 months ago

bw7715 commented 7 months ago

Describe the bug An error occurs when try to freeze a project with torchvision CUDA module.

copying C:\Program Files\Python39\tcl\tk8.6\ttk\xpTheme.tcl -> C:\Project\cx_freeze_issues\p39_torch221\_build\out\lib\tk8.6\ttk\xpTheme.tcl
creating directory C:\Project\cx_freeze_issues\p39_torch221\_build\out\lib\numpy.libs
copying C:\Project\cx_freeze_issues\p39_torch221\_build\venv\lib\site-packages\numpy.libs\.load-order-numpy-1.26.4 -> C:\Project\cx_freeze_issues\p39_torch221\_build\out\lib\numpy.libs\.load-order-numpy-1.26.4
copying C:\Project\cx_freeze_issues\p39_torch221\_build\venv\lib\site-packages\numpy.libs\libopenblas64__v0.3.23-293-gc2f4bdbb-gcc_10_3_0-2bde3a66a51006b2b53eb373ff767a3f.dll -> C:\Project\cx_freeze_issues\p39_torch221\_build\out\lib\numpy.libs\libopenblas64__v0.3.23-293-gc2f4bdbb-gcc_10_3_0-2bde3a66a51006b2b53eb373ff767a3f.dll
WARNING: cannot find 'api-ms-win-crt-private-l1-1-0.dll'
Traceback (most recent call last):
  File "C:\Project\cx_freeze_issues\p39_torch221\_build\venv\Lib\site-packages\cx_Freeze\initscripts\__startup__.py", line 124, in run
    module_init.run(name + "__main__")
  File "C:\Project\cx_freeze_issues\p39_torch221\_build\venv\Lib\site-packages\cx_Freeze\initscripts\console.py", line 16, in run
    exec(code, module_main.__dict__)
  File "project.py", line 1, in <module>
    import torchvision
  File "C:\Project\cx_freeze_issues\p39_torch221\_build\venv\lib\site-packages\torchvision\__init__.py", line 6, in <module>
    from torchvision import _meta_registrations, datasets, io, models, ops, transforms, utils
  File "C:\Project\cx_freeze_issues\p39_torch221\_build\venv\lib\site-packages\torchvision\models\__init__.py", line 2, in <module>
    from .convnext import *
  File "C:\Project\cx_freeze_issues\p39_torch221\_build\venv\lib\site-packages\torchvision\models\convnext.py", line 8, in <module>
    from ..ops.misc import Conv2dNormActivation, Permute
  File "C:\Project\cx_freeze_issues\p39_torch221\_build\venv\lib\site-packages\torchvision\ops\__init__.py", line 23, in <module>
    from .poolers import MultiScaleRoIAlign
  File "C:\Project\cx_freeze_issues\p39_torch221\_build\venv\lib\site-packages\torchvision\ops\poolers.py", line 10, in <module>
    from .roi_align import roi_align
  File "C:\Project\cx_freeze_issues\p39_torch221\_build\venv\lib\site-packages\torchvision\ops\roi_align.py", line 4, in <module>
    import torch._dynamo
  File "C:\Project\cx_freeze_issues\p39_torch221\_build\venv\lib\site-packages\torch\_dynamo\__init__.py", line 2, in <module>
    from . import allowed_functions, convert_frame, eval_frame, resume_execution
  File "C:\Project\cx_freeze_issues\p39_torch221\_build\venv\lib\site-packages\torch\_dynamo\allowed_functions.py", line 28, in <module>
    from . import config
  File "C:\Project\cx_freeze_issues\p39_torch221\_build\venv\lib\site-packages\torch\_dynamo\config.py", line 344, in <module>
    install_config_module(sys.modules[__name__])
  File "C:\Project\cx_freeze_issues\p39_torch221\_build\venv\lib\site-packages\torch\utils\_config_module.py", line 57, in install_config_module
    compile_ignored_keys = get_assignments_with_compile_ignored_comments(module)
  File "C:\Project\cx_freeze_issues\p39_torch221\_build\venv\lib\site-packages\torch\utils\_config_module.py", line 74, in get_assignments_with_compile_ignored_comments
    source_code = inspect.getsource(module)
  File "C:\Program Files\Python39\lib\inspect.py", line 1024, in getsource
    lines, lnum = getsourcelines(object)
  File "C:\Program Files\Python39\lib\inspect.py", line 1006, in getsourcelines
    lines, lnum = findsource(object)
  File "C:\Program Files\Python39\lib\inspect.py", line 835, in findsource
    raise OSError('could not get source code')
OSError: could not get source code

To Reproduce (project.py)

import torchvision
print(torchvision.__version__)

(requirements.txt)

--find-links https://download.pytorch.org/whl/cu118/torch_stable.html

cx_Freeze==6.15.15
torch==2.2.1+cu118
torchvision==0.17.1+cu118

(freeze script)

echo off
set WORKSPACE=%~dp0

set BUILD_DIRPATH=%WORKSPACE%\_build
set BASE_PYTHON_DIRPATH=C:\Program Files\Python39

set VENV_DIRPATH=%BUILD_DIRPATH%\venv
set TARGET_DIR=%BUILD_DIRPATH%\out

set SCRIPTS_DIRPATH=%VENV_DIRPATH%\Scripts
set PYTHON_EXE_PATH="%SCRIPTS_DIRPATH%\python.exe"
set FREEZER_EXE_PATH="%SCRIPTS_DIRPATH%\cxfreeze.exe"

set TARGET_NAME=project_out.exe
set PROJECT_SOURCE_PY_PATH=project.py

set PACKAGES=torch,torchvision

"%BASE_PYTHON_DIRPATH%\python" -m venv "%VENV_DIRPATH%"
%PYTHON_EXE_PATH% -m pip install -r requirements.txt

rem %PYTHON_EXE_PATH% -m pip install --pre --extra-index-url https://marcelotduarte.github.io/packages/ cx_Freeze --upgrade

%FREEZER_EXE_PATH% -O -OO --include-msvcr --target-dir="%TARGET_DIR%" --target-name=%TARGET_NAME% --packages=%PACKAGES% %PROJECT_SOURCE_PY_PATH%

"%TARGET_DIR%\%TARGET_NAME%"

if errorlevel 1 pause

Desktop (please complete the following information):

Additional context Add any other context about the problem here.

marcelotduarte commented 7 months ago

You can test the patch in the latest development build (dev63): pip install --force --no-cache --pre --extra-index-url https://marcelotduarte.github.io/packages/ cx_Freeze

With this patch, the use of --packages can be removed. Tested the sample with torch 2.1 and 2.2 (torchvision 0.16 and 0.17).

bw7715 commented 7 months ago

Has anything changed in the order of arguments in the cx-freeze command? When I run the current version the freeze starts, but when I run the developer version (6.16.0.dev63) I get an error: invalid command name 'project.py'

marcelotduarte commented 7 months ago

cxfreeze --script=project.py

MarceloUnlibreProjects commented 6 months ago

quando instalo essa versao dev63, resolve o problema do "could not get source code". Porém, aparece o problema "module 'abc' has no attribute ' file '" como resolver? isso é usando o easyocr, que também usa o torch (2.2.1) e torchvision 0.17.1

bw7715 commented 6 months ago

Thank you with 6.16.0.dev63 the torchvision 0.17.1 works.

I updated my freezer script. The old one: %FREEZER_EXE_PATH% -O -OO --include-msvcr --target-dir="%TARGET_DIR%" --target-name=%TARGET_NAME% --packages=%PACKAGES% project.py

The new one: %FREEZER_EXE_PATH% --include-msvcr build --build-exe="%TARGET_DIR%" --target-name=%TARGET_NAME% --script=project.py

But I'm not sure where to put the -O -OO parameters?

marcelotduarte commented 6 months ago

@MarceloUnlibreProjects Depois vou dar uma olhada, vou pegar um exemplo do easyocr. @bw7715 Use: cxfreeze --target-name=%TARGET_NAME% --script=project.py build_exe -OO --include-msvcr --build-exe="%TARGET_DIR%"

For more info: cxfreeze --help cxfreeze build_exe --help

bw7715 commented 6 months ago

With cxfreeze --target-name=%TARGET_NAME% --script=project.py build_exe -OO --include-msvcr --build-exe="%TARGET_DIR%" I get an error: ValueError: invalid literal for int() with base 10: 'O'

running build_exe
Traceback (most recent call last):
  File "C:\Program Files\Python39\lib\runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Program Files\Python39\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "C:\Project\p39_torch221\_build\venv\Scripts\cxfreeze.exe\__main__.py", line 7, in <module>
  File "C:\Project\p39_torch221\_build\venv\lib\site-packages\cx_Freeze\cli.py", line 226, in main
    setup(
  File "C:\Project\p39_torch221\_build\venv\lib\site-packages\cx_Freeze\__init__.py", line 69, in setup
    return setuptools.setup(**attrs)
  File "C:\Project\p39_torch221\_build\venv\lib\site-packages\setuptools\__init__.py", line 103, in setup
    return distutils.core.setup(**attrs)
  File "C:\Project\p39_torch221\_build\venv\lib\site-packages\setuptools\_distutils\core.py", line 185, in setup
    return run_commands(dist)
  File "C:\Project\p39_torch221\_build\venv\lib\site-packages\setuptools\_distutils\core.py", line 201, in run_commands
    dist.run_commands()
  File "C:\Project\p39_torch221\_build\venv\lib\site-packages\setuptools\_distutils\dist.py", line 969, in run_commands
    self.run_command(cmd)
  File "C:\Project\p39_torch221\_build\venv\lib\site-packages\setuptools\dist.py", line 963, in run_command
    super().run_command(command)
  File "C:\Project\p39_torch221\_build\venv\lib\site-packages\setuptools\_distutils\dist.py", line 987, in run_command
    cmd_obj.ensure_finalized()
  File "C:\Project\p39_torch221\_build\venv\lib\site-packages\setuptools\_distutils\cmd.py", line 111, in ensure_finalized
    self.finalize_options()
  File "C:\Project\p39_torch221\_build\venv\lib\site-packages\cx_Freeze\command\build_exe.py", line 249, in finalize_options
    self.optimize = int(self.optimize)
ValueError: invalid literal for int() with base 10: 'O'
bw7715 commented 6 months ago

I replaced -O and -OO with -O1 and -O2 and them freezing start but I get an error:

copying C:\Project\p39_torch221\_build\venv\lib\site-packages\torch\fx\config.py -> C:\Project\p39_torch221\_build\out\lib\torch\fx\config.py
copying C:\Project\p39_torch221\_build\venv\lib\site-packages\torch\fx\experimental\_config.py -> C:\Project\p39_torch221\_build\out\lib\torch\fx\experimental\_config.py
Traceback (most recent call last):
  File "C:\Project\p39_torch221\_build\venv\Lib\site-packages\cx_Freeze\initscripts\__startup__.py", line 132, in run
    module_init.run(name + "__main__")
  File "C:\Project\p39_torch221\_build\venv\Lib\site-packages\cx_Freeze\initscripts\console.py", line 17, in run
    exec(code, module_main.__dict__)
  File "project.py", line 1, in <module>
    import torchvision
  File "C:\Project\p39_torch221\_build\venv\lib\site-packages\torchvision\__init__.py", line 5, in <module>
    import torch
  File "C:/Users/bw7715/OneDrive - Zebra Technologies/Projects/python_projects/cx_freeze_issues/p39_torch221/_build/venv/lib/site-packages/torch/__init__.py", line 1215, in <module>
    from .storage import _StorageBase, TypedStorage, _LegacyStorage, UntypedStorage, _warn_typed_storage_removal
  File "C:\Project\p39_torch221\_build\venv\lib\site-packages\torch\storage.py", line 14, in <module>
    import numpy as np
  File "C:\Project\p39_torch221\_build\venv\lib\site-packages\numpy\__init__.py", line 173, in <module>
    from . import core
  File "C:\Project\p39_torch221\_build\venv\lib\site-packages\numpy\core\__init__.py", line 24, in <module>
    from . import multiarray
  File "C:\Project\p39_torch221\_build\venv\lib\site-packages\numpy\core\multiarray.py", line 86, in <module>
    def empty_like(prototype, dtype=None, order=None, subok=None, shape=None):
  File "C:\Project\p39_torch221\_build\venv\lib\site-packages\numpy\core\overrides.py", line 178, in decorator
    return array_function_dispatch(
  File "C:\Project\p39_torch221\_build\venv\lib\site-packages\numpy\core\overrides.py", line 158, in decorator
    add_docstring(implementation, dispatcher.__doc__)
TypeError: argument docstring of add_docstring should be a str
marcelotduarte commented 6 months ago

numpy uses its docstring, so remove the optimization that it works. The optimization works like described here. See also: https://github.com/numpy/numpy/issues/13248#issuecomment-480412876

MarceloUnlibreProjects commented 6 months ago

meu arquivo setup, está assim: (também tentei botando o easyocr em packages, mas deu no mesmo)

from cx_Freeze import setup, Executable

build_exe_options = {
    "includes": ["requests", "psutil", "pyrebase", "win32gui", "cv2", "numpy", "socks", "windows_toasts", "pygetwindow", "bs4", "easyocr"] 
}

executables = [
    Executable(script=r"test1.py", base=None, icon='iapp.ico', target_name="exec1.exe"),
    Executable(script=r"test2.py", base=None, icon='iapp.ico', target_name="exec2.exe")
]

setup(  name = "test",
        version = "0.8",
        description = "test",
        options = {"build_exe": build_exe_options},
        executables = executables)
MarceloUnlibreProjects commented 6 months ago

o erro só acontece quando tenta importar o módulo easyocr, dentro do exe

marcelotduarte commented 6 months ago

@MarceloUnlibreProjects You can test the patch (easyocr hook) in the latest development build (dev67): pip install --force --no-cache --pre --extra-index-url https://marcelotduarte.github.io/packages/ cx_Freeze

Eu não consegui reproduzir o erro que vc teve, mas vi alguns problemas. Usei o seguinte para testar:

import easyocr
reader = easyocr.Reader(['ch_sim','en'], gpu=False)
result = reader.readtext('chinese.jpg')
print(result)

Se vc continuar tento erro, por favor abre uma issue e forneca um sample (ou modifique o que postei) que consiga reproduzir o erro.

marcelotduarte commented 6 months ago

Release 6.15.16 is out!