无法 运行 使用 cx-freeze 创建的 exe

Can't run exe created with cx-freeze

我一直在使用 cx-freeze 从一组 Python 脚本创建可执行文件。 setup.py 如下所示

from cx_Freeze import setup, Executable

setup(name='foo',
      version='1.0',
      description='some description',
      options={'build_exe': {'includes': ['numpy.core._methods',
                                          'numpy.lib.format',
                                          'matplotlib'],
                             'packages': ['matplotlib.backends.backend_agg']}},
      executables=[Executable('main.py', targetName="foo.exe")])

然后我从命令行调用构建

python setup.py build

这成功并创建了可执行文件以及所需的依赖项。但是,当我尝试 运行 应用程序时,我看到以下内容(路径已被修改以删除个人信息)

> build\foo.exe
Traceback (most recent call last):
  File "{long_path}\venv\lib\site-packages\cx_Freeze\initscripts\__startup__.py", line 14, in run
    module.run()
  File "{long_path}\venv\lib\site-packages\cx_Freeze\initscripts\Console.py", line 26, in run
    exec(code, m.__dict__)
  File "main.py", line 11, in <module>
  File "{root_path}\plot.py", line 5, in <module>
    import matplotlib
  File "{long_path}\venv\lib\site-packages\matplotlib\__init__.py", line 109, in <module>
    import distutils.version
  File "{long_path}\venv\lib\distutils\__init__.py", line 17, in <module>
    real_distutils = imp.load_module("_virtualenv_distutils", None, distutils_path, ('', '', imp.PKG_DIRECTORY))
  File "{long_path}\venv\lib\imp.py", line 245, in load_module
    return load_package(name, filename)
  File "{long_path}\venv\lib\imp.py", line 217, in load_package
    return _load(spec)
  File "<frozen importlib._bootstrap>", line 692, in _load
AttributeError: 'NoneType' object has no attribute 'name'

我的 setup.py 有什么不正确的地方吗?我该怎么做才能更正此应用程序的构建?

版本:

Python 3.5.4 (v3.5.4:3f56838, Aug  8 2017, 02:17:05) [MSC v.1900 64 bit (AMD64)] on win32
cx-Freeze==5.1.1

显然,这是使用 cx-freezedistutilsvirtualenv 组合时的一个已知错误(参见 here)。

从上面link:

This problem happens because distutils does not install all its modules into the virtualenv, it only creates a package with some magic code in the __init__ file to import its submodules dynamically. This is a problem to cx-freeze's static module analysis, which complains during the build command that it can't find distutils modules.

上面的解决方案(解决方法)link:

使用的解决方法是告诉 cx-freeze 排除 distutils 并从原始解释器(而不是 virtualenv)手动添加包。

# contents of setup.py
from cx_Freeze import setup, Executable

import distutils
import opcode
import os

# opcode is not a virtualenv module, so we can use it to find the stdlib; this is the same
# trick used by distutils itself it installs itself into the virtualenv
distutils_path = os.path.join(os.path.dirname(opcode.__file__), 'distutils')
build_exe_options = {'include_files': [(distutils_path, 'lib/distutils')],
                     'excludes': ['distutils']}

setup(
    name="foo",
    version="0.1",
    description="My app",
    options={"build_exe": build_exe_options},
    executables=[Executable("foo_main.py", base=None)],
)