Im trying to create a basic exe using cx_Freeze. It works for .py programs that don’t have numpy but I can’t get one made correctly with numpy.
*Any ideas on how to fix this? is there something i need to include in my setup.py?
When I go to run the exe it says:
c:Python32Scriptsdist>Assignment4_5.exe
Traceback (most recent call last):
File "C:Python32libsite-packagescx_FreezeinitscriptsConsole3.py", line 2
7, in <module>
exec(code, m.__dict__)
File "c:Python32Assignment4_5.py", line 6, in <module>
import numpy as np
File "C:Python32libsite-packagesnumpy__init__.py", line 137, in <module>
from . import add_newdocs
File "C:Python32libsite-packagesnumpyadd_newdocs.py", line 9, in <module>
from numpy.lib import add_newdoc
File "C:Python32libsite-packagesnumpylib__init__.py", line 17, in <modul
e>
from .npyio import *
File "C:Python32libsite-packagesnumpylibnpyio.py", line 6, in <module>
from . import format
ImportError: cannot import name format
c:Python32Scriptsdist>
Setup.py:
from cx_Freeze import setup, Executable
includeDependencies = []
setup(
name = "Assignment4_5PythonExe",
version = "0.1",
description = "Sort Methods",
executables = [Executable("Assignment4_5.py")]
)
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
This is a bug in cx_Freeze – it doesn’t automatically detect that it should copy the module numpy.lib.format. It’s already fixed in the development version, so if you’re in a position to try that, it should work.
Otherwise, you’ll need to specify that numpy.lib.format needs to be included in your setup.py. The line will look something like this:
options = {"build_exe": {"packages": ["numpy.lib.format"]}},
See also the documentation.
Method 2
Numpy seems to be a little confusing to cx_Freeze so you need to declare it explicitly. As referenced in the docs
Here is your solution:
from cx_Freeze import setup, Executable
build_exe_options = {"packages": ["numpy"]}
setup(
name = "Assignment4_5PythonExe",
version = "0.1",
description = "Sort Methods",
options = {"build_exe": build_exe_options},
executables = [Executable("Assignment4_5.py")]
)
Method 3
I recently encountered this issue using cx_freeze 6.1 and Python 3.5.4. In order to solve my runtime issues, I had to add the numpy lib path to the system paths at runtime. Here is the relevant code snippet if it can help anyone:
if __name__ == '__main__':
# The frozen app needs the numpy path added to it's file.
try:
this_file = __file__
except NameError:
this_file = sys.argv[0]
sys.path.insert(0, str(Path(this_file).absolute().parent / "lib" / "numpy"))
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0