无法运行用cxfreez创建的exe

2024-04-25 17:42:00 发布

您现在位置:Python中文网/ 问答频道 /正文

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

然后我从命令行调用构建

^{pr2}$

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

> 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

Tags: pathinpyvenvmatplotliblibsetupline
1条回答
网友
1楼 · 发布于 2024-04-25 17:42:00

显然,当使用cx-freezedistutils和{}的组合时,这是一个已知的错误(参见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排除{},并从原始解释器(而不是从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)],
)

相关问题 更多 >