使用Numpy for Python创建cx_Freeze exe

时间:2012-04-04 05:43:15

标签: python numpy cx-freeze

我试图使用cx_Freeze创建一个基本的exe。它适用于没有numpy的.py程序,但我无法通过numpy正确制作。

*有关如何解决此问题的任何想法?我需要在setup.py中包含哪些内容?

当我去运行exe时,它说:

           c:\Python32\Scripts\dist>Assignment4_5.exe
           Traceback (most recent call last):
     File "C:\Python32\lib\site-packages\cx_Freeze\initscripts\Console3.py", line 2
     7, in <module>
     exec(code, m.__dict__)
     File "c:\Python32\Assignment4_5.py", line 6, in <module>
     import numpy as np
     File "C:\Python32\lib\site-packages\numpy\__init__.py", line 137, in <module>
     from . import add_newdocs
     File "C:\Python32\lib\site-packages\numpy\add_newdocs.py", line 9, in <module>

     from numpy.lib import add_newdoc
     File "C:\Python32\lib\site-packages\numpy\lib\__init__.py", line 17, in <modul
     e>
    from .npyio import *
    File "C:\Python32\lib\site-packages\numpy\lib\npyio.py", line 6, in <module>
    from . import format
    ImportError: cannot import name format

   c:\Python32\Scripts\dist>

Setup.py:

   from cx_Freeze import setup, Executable

   includeDependencies = []

   setup(
        name = "Assignment4_5PythonExe",
        version = "0.1",
        description = "Sort Methods",
        executables = [Executable("Assignment4_5.py")]
        )

3 个答案:

答案 0 :(得分:5)

这是cx_Freeze中的一个错误 - 它不会自动检测到它应该复制模块numpy.lib.format。它已在development version中修复,因此,如果您有能力尝试,则应该有效。

否则,您需要指定numpy.lib.format需要包含setup.py。该行看起来像这样:

options = {"build_exe": {"packages": ["numpy.lib.format"]}},

另见the documentation

答案 1 :(得分:5)

Numpy似乎对cx_Freeze有点困惑所以你需要明确声明它。正如docs

中所述

以下是您的解决方案:

   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")]
        )

答案 2 :(得分:0)

我最近在使用cx_freeze 6.1和Python 3.5.4时遇到了此问题。为了解决我的运行时问题,我不得不在运行时将numpy lib路径添加到系统路径中。如果可以帮助任何人,请参见以下相关代码段:

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"))