Search code examples
pythonpython-3.xnumpycx-freeze

Numpy incompatibility with cx_freeze


I'm in the process of making a program executable with cx_Freeze. This program uses several libraries such as matplotlib and scipy. When I create my executable with my "setup.py", I get this error: RecursionError: maximum recursion depth exceeded during compilation I then ran several tests to find out which library was causing the problem, and it was numpy. When I exclude numpy from my setup.py, the executable file is created, but when I run it, I get an error telling me that numpy has not been imported, since matplotlib and scipy use it. Here's my setup.py :

import sys
from cx_Freeze import setup, Executable

# Dependencies are automatically detected, but it might need fine tuning.
build_exe_options = {
    "includes": ["tkinter", "matplotlib", "pyvisa", "PIL", "cmath", "sympy", "scipy"],
    # "excludes": ["numpy"],    
    "zip_include_packages": ["encodings", "PySide6", "numpy"],
}


# base="Win32GUI" should be used only for Windows GUI app
base = "Win32GUI" if sys.platform == "win32" else None

setup(
    name="App",
    version="0.1",
    description="Software",
    options={"build_exe": build_exe_options},
    executables=[Executable("main.py", base=base, icon="icon.ico")],
)

I'm trying to import manually numpy but i don't know how. Is there a way to increase the number of possible recursions, like with pyinstaller and .spec ?


Solution

  • Well, I've just found the solution to my problem. I've tried increasing the number of possible recursions as with pyinstaller's .spec. By adding exactly the same line of code in my "setup.py" file, I can have my executable created and I no longer have any compatibility problems. Here's my working code :

    import sys
    from cx_Freeze import setup, Executable
    
    # Dependencies are automatically detected, but it might need fine tuning.
    build_exe_options = {
        "includes": ["tkinter", "matplotlib", "pyvisa", "PIL", "cmath", "sympy", "scipy", "numpy"],
        "zip_include_packages": ["encodings", "PySide6", "numpy"],
    }
    
    sys.setrecursionlimit(sys.getrecursionlimit() * 5)
    
    # base="Win32GUI" should be used only for Windows GUI app
    base = "Win32GUI" if sys.platform == "win32" else None
    
    setup(
        name="App",
        version="0.1",
        description="Software",
        options={"build_exe": build_exe_options},
        executables=[Executable("main.py", base=base, icon="icon.ico")],
    )