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

cx-Freeze - No module named 'scipy._lib.array_api_compat._aliases' #2596

Open dcnieho opened 2 hours ago

dcnieho commented 2 hours ago

Prerequisite This was previously reported in the closed issue #2544, where no action was taken. I include a minimal script that produces the problem for me.

Describe the bug When running the compiled executable, i get the following error:

PS C:\dat\projects\gazeMapper\cxFreeze\build\exe.win-amd64-3.10> .\test.exe
Traceback (most recent call last):
  File "C:\dat\projects\gazeMapper\cxFreeze\.venv\Lib\site-packages\cx_Freeze\initscripts\__startup__.py", line 141, in run
    module_init.run(f"__main__{name}")
  File "C:\dat\projects\gazeMapper\cxFreeze\.venv\Lib\site-packages\cx_Freeze\initscripts\console.py", line 25, in run
    exec(code, main_globals)
  File "C:\dat\projects\gazeMapper\cxFreeze\test.py", line 1, in <module>
    from scipy.spatial.transform import Rotation
  File "C:\dat\projects\gazeMapper\cxFreeze\.venv\lib\site-packages\scipy\spatial\__init__.py", line 110, in <module>
    from ._kdtree import *
  File "C:\dat\projects\gazeMapper\cxFreeze\.venv\lib\site-packages\scipy\spatial\_kdtree.py", line 4, in <module>
    from ._ckdtree import cKDTree, cKDTreeNode
  File "_ckdtree.pyx", line 11, in init scipy.spatial._ckdtree
  File "C:\dat\projects\gazeMapper\cxFreeze\.venv\lib\site-packages\scipy\sparse\__init__.py", line 293, in <module>
    from ._base import *
  File "C:\dat\projects\gazeMapper\cxFreeze\.venv\lib\site-packages\scipy\sparse\_base.py", line 5, in <module>
    from ._sputils import (asmatrix, check_reshape_kwargs, check_shape,
  File "C:\dat\projects\gazeMapper\cxFreeze\.venv\lib\site-packages\scipy\sparse\_sputils.py", line 10, in <module>
    from scipy._lib._util import np_long, np_ulong
  File "C:\dat\projects\gazeMapper\cxFreeze\.venv\lib\site-packages\scipy\_lib\_util.py", line 18, in <module>
    from scipy._lib._array_api import array_namespace, is_numpy, size as xp_size
  File "C:\dat\projects\gazeMapper\cxFreeze\.venv\lib\site-packages\scipy\_lib\_array_api.py", line 21, in <module>
    from scipy._lib.array_api_compat import (
  File "C:\dat\projects\gazeMapper\cxFreeze\.venv\lib\site-packages\scipy\_lib\array_api_compat\numpy\__init__.py", line 16, in <module>
    __import__(__package__ + '.linalg')
ModuleNotFoundError: No module named 'scipy._lib.array_api_compat._aliases'

To Reproduce Two files: test.py

from scipy.spatial.transform import Rotation
print(Rotation.from_euler('XYZ', [10, 10, 10], degrees=True).as_matrix())

setup.py:

import cx_Freeze
import pathlib
import sys
import site

path = pathlib.Path(__file__).absolute().parent

def get_include_files():
    # don't know if this is a bad idea, it certainly didn't help
    files = []

    # scipy dlls
    for d in site.getsitepackages():
        d=pathlib.Path(d)/'scipy'/'.libs'
        if d.is_dir():
            for f in d.iterdir():
                if f.is_file() and f.suffix=='' or f.suffix in ['.dll']:
                    files.append((f,pathlib.Path('lib')/f.name))
    return files

build_options = {
    "build_exe": {
        "optimize": 1,
        "packages": [
            'numpy','scipy'
        ],
        "excludes":["tkinter"],
        "zip_include_packages": "*",
        "zip_exclude_packages": [],
        "silent_level": 1,
        "include_msvcr": True
    }
}
if sys.platform.startswith("win"):
    build_options["build_exe"]["include_files"] = get_include_files()

cx_Freeze.setup(
    name="test",
    version="0.0.1",
    description="test",
    executables=[
        cx_Freeze.Executable(
            script=path / "test.py",
            target_name="test"
        )
    ],
    options=build_options,
    py_modules=[]
)

Expected behavior exe runs

Desktop (please complete the following information):

Additional context at \.venv\Lib\site-packages\scipy\_lib\array_api_compat there is no _aliases.py, only __init__.py with the following content:

__version__ = '1.5.1'
from .common import *  # noqa: F401, F403

_aliases.py does exist at \.venv\Lib\site-packages\scipy\_lib\array_api_compat\common

Both files are packed into library.zip (whole scipy tree is)

dcnieho commented 2 hours ago

Changing the config to "zip_exclude_packages": ['scipy'], things work. I assume it should work just fine/the same from the zip file. This will be my workaround for now