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.36k stars 220 forks source link

Torchmetrics: ModuleNotFoundError: No module named 'matplotlib' #2631

Closed bw7715 closed 3 weeks ago

bw7715 commented 1 month ago

Prerequisite When try to exclude 'matlotlib' module from final project an error occurs.

Describe the bug

Traceback (most recent call last):
  File "C:\Projects\p39_matplotlib\_build\venv\Lib\site-packages\cx_Freeze\initscripts\__startup__.py", line 141, in run
    module_init.run(name + "__main__")
  File "C:\Projects\p39_matplotlib\_build\venv\Lib\site-packages\cx_Freeze\initscripts\console.py", line 25, in run
    exec(code, main_globals)
  File "C:\Projects\p39_matplotlib\project.py", line 1, in <module>
    import torchmetrics
  File "C:\Projects\p39_matplotlib\_build\venv\lib\site-packages\torchmetrics\__init__.py", line 30, in <module>
    from torchmetrics import functional  # noqa: E402
  File "C:\Projects\p39_matplotlib\_build\venv\lib\site-packages\torchmetrics\functional\__init__.py", line 14, in <module>
    from torchmetrics.functional.audio._deprecated import _permutation_invariant_training as permutation_invariant_training
  File "C:\Projects\p39_matplotlib\_build\venv\lib\site-packages\torchmetrics\functional\audio\__init__.py", line 14, in <module>
    from torchmetrics.functional.audio.pit import permutation_invariant_training, pit_permutate
  File "C:\Projects\p39_matplotlib\_build\venv\lib\site-packages\torchmetrics\functional\audio\pit.py", line 22, in <module>
    from torchmetrics.utilities import rank_zero_warn
  File "C:\Projects\p39_matplotlib\_build\venv\lib\site-packages\torchmetrics\utilities\__init__.py", line 14, in <module>
    from torchmetrics.utilities.checks import check_forward_full_state_property
  File "C:\Projects\p39_matplotlib\_build\venv\lib\site-packages\torchmetrics\utilities\checks.py", line 25, in <module>
    from torchmetrics.metric import Metric
  File "C:\Projects\p39_matplotlib\_build\venv\lib\site-packages\torchmetrics\metric.py", line 42, in <module>
    from torchmetrics.utilities.plot import _AX_TYPE, _PLOT_OUT_TYPE, plot_single_or_multi_val
  File "C:\Projects\p39_matplotlib\_build\venv\lib\site-packages\torchmetrics\utilities\plot.py", line 25, in <module>
    import matplotlib
ModuleNotFoundError: No module named 'matplotlib'

To Reproduce project.py

import torchmetrics
print(torchmetrics.__version__)

freeze.bat

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="%WORKSPACE%project.py"

set PACKAGES=torchmetrics
set EXCLUDES=matplotlib

"%BASE_PYTHON_DIRPATH%\python" -m venv "%VENV_DIRPATH%"
%PYTHON_EXE_PATH% -m pip install -r requirements.txt
rem %PYTHON_EXE_PATH% -m pip install --force --no-cache --pre --extra-index-url https://marcelotduarte.github.io/packages/ cx_Freeze
if errorlevel 1 pause

@REM %PYTHON_EXE_PATH% -m pip uninstall -y matplotlib

%FREEZER_EXE_PATH% build -O1 -O2 --include-msvcr --build-exe="%TARGET_DIR%" --target-name=%TARGET_NAME% --packages=%PACKAGES% --excludes=%EXCLUDES% --script=%PROJECT_SOURCE_PY_PATH%
if errorlevel 1 pause

"%TARGET_DIR%\%TARGET_NAME%"
if errorlevel 1 pause

requirements.txt

cx_Freeze
torchmetrics==1.4.2
numpy==1.26.4
matplotlib

But if I uninstall matplotlib from venv before freezing the project works.

%PYTHON_EXE_PATH% -m pip uninstall -y matplotlib

Desktop (please complete the following information):

bw7715 commented 1 month ago

Inside torchmetrics module is a check if matplotlib module is available.

\torchmetrics\utilities\plot.py

...
from torchmetrics.utilities.imports import _LATEX_AVAILABLE, _MATPLOTLIB_AVAILABLE, _SCIENCEPLOT_AVAILABLE

if _MATPLOTLIB_AVAILABLE:
    import matplotlib
    import matplotlib.axes
    import matplotlib.pyplot as plt
...
bw7715 commented 1 month ago

VENV:

.\python.exe -m pip list
Package             Version
------------------- -----------
contourpy           1.3.0
cx_Freeze           7.2.3
cx_Logging          3.2.1
cycler              0.12.1
filelock            3.16.1
fonttools           4.54.1
fsspec              2024.9.0
importlib_metadata  8.5.0
importlib_resources 6.4.5
Jinja2              3.1.4
kiwisolver          1.4.7
lief                0.15.1
lightning-utilities 0.11.8
MarkupSafe          3.0.1
matplotlib          3.9.2
mpmath              1.3.0
networkx            3.2.1
numpy               1.26.4
packaging           24.1
pillow              11.0.0
pip                 22.0.4
pyparsing           3.2.0
python-dateutil     2.9.0.post0
setuptools          75.1.0
six                 1.16.0
sympy               1.13.3
tomli               2.0.2
torch               2.4.1
torchmetrics        1.4.2
typing_extensions   4.12.2
zipp                3.20.2
marcelotduarte commented 1 month ago

You can test the PR with: pip install --no-cache --pre git+https://github.com/marcelotduarte/cx_Freeze.git@refs/pull/2634/head --force If using uv: uv pip install --no-cache --pre git+https://github.com/marcelotduarte/cx_Freeze.git@refs/pull/2634/head --force-reinstall

bw7715 commented 3 weeks ago

I try to test with a: pip install --no-cache --pre git+https://github.com/marcelotduarte/cx_Freeze.git@refs/pull/2634/head --force

but with a command: %FREEZER_EXE_PATH% build -O1 -O2 --include-msvcr --build-exe="%TARGET_DIR%" --target-name=%TARGET_NAME% --packages=%PACKAGES% --excludes=%EXCLUDES% --script=%PROJECT_SOURCE_PY_PATH%

I get an error during freezing process:

running build
running build_exe
error: [Errno 22] Invalid argument

with cx_Freeze==7.2.0 I don't have this problem.

bw7715 commented 3 weeks ago

With 7.2.1 and 7.2.2 I also don't have this issue.

But the problem appeared in version 7.2.3. Has something changed in the use of parameters?

marcelotduarte commented 2 weeks ago

I redid the tests, tested with torch 2.5.0 and 2.4.1 I also tested, with matplotlib installed, versions 7.2.1, 7.2.2 and 7.2.3. They all failed. I tested with the PR and the main version and both passed. So I suggest: pip uninstall cx_Freeze pip install --no-cache --pre git+https://github.com/marcelotduarte/cx_Freeze.git@main

Note: error: [Errno 22] Invalid argument is a OSError.

Has something changed in the use of parameters?

No. No change in command line parameters. Argument refers to the argument of a function, for example: https://stackoverflow.com/questions/25584124/oserror-errno-22-invalid-argument-when-use-open-in-python

marcelotduarte commented 2 weeks ago

Release 7.2.4 is out! Documentation

Please give me feedback.