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

标签 python virtualenv 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 )。

来自上面的链接:

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.

来自上述链接的解决方案(解决方法):

使用的解决方法是告诉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)],
)

关于python - 无法运行使用 cx-freeze 创建的 exe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48502648/

相关文章:

python - 从列表和其他单个值创建 numpy 数组的最佳方法

python - 通过代码执行python文件

python-3.x - 如何检查安装了哪个版本的 Virtual Env

python - 使用 PyPy 作为 PyCharm 的解释器

python - 将脚本转换为可执行文件,包括远程文件夹中的数据文件

python - cx_freeze 在带有视网膜显示屏的 MacBook Pro 上生成模糊 GUI

python - 是否可以使用 scikit-learn 而不是二元分类来预测变量(如果可以)而不是如何预测

python - Selenium 类型错误 : __init__() takes 2 positional arguments but 3 were given

python - 在 Django 中使用 virtualenv 时出现 ImportError

python - cx_Freeze - opencv 兼容性