【问题标题】:Can't run exe created with cx-freeze无法运行使用 cx-freeze 创建的 exe
【发布时间】:2023-03-21 11:05:01
【问题描述】:

我一直在使用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

【问题讨论】:

标签: python virtualenv cx-freeze


【解决方案1】:

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

从上面的链接:

出现这个问题是因为distutils 没有将它的所有模块都安装到virtualenv 中,它只是在__init__ 文件中创建了一个带有一些魔术代码的包来动态导入它的子模块。这是 cx-freeze 的静态模块分析的问题,它在 build 命令期间抱怨它找不到 distutils 模块。

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多