ImportError: cannot import name packages

ImportError: cannot import name packages

我正在使用 cx_Freeze 版本 5.1.1,并且我有一个 python 2.7.9 应用程序要使用 cx_Freeze 打包。该应用程序正在使用 python 'requests' 模块(请求版本为 2.18.4)

这是我的 cx_Freeze setup.py 文件(注意我在要包含的包中特别包含 'requests'):

from cx_Freeze import setup, Executable
import sys

from cx_Freeze import setup, Executable

packages_to_include =['lib','lib/DB','encodings.ascii','requests']

buildOptions = dict(
    optimize=1,
    excludes = ['tkinter'],
    bin_includes = [
        'libcrypto.so.1.0.0',
        'libcrypto.so.10',
        'libgssapi_krb5.so.2',
        'libk5crypto.so.3',
        'libkeyutils.so.1',
        'libssl.so.1.0.1e',
        'libssl.so.10'
    ],
    includes = packages_to_include,
    packages= ['urllib3','idna']
)

executables = [
    Executable(
        'workapp.py',
        targetName = 'workapp'
    )
]

setup(
    name='Sample Flask App',
    version = '0.1',
    description = 'Sample Flask App',
    #requires = ["requests"],
    options = dict(build_exe = buildOptions),
    executables = executables
)

构建已成功创建可执行文件,但在 运行 我的应用程序出现以下错误:

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/cx_Freeze/initscripts/__startup__.py", line 14, in run
    module.run()
  File "/usr/local/lib/python2.7/site-packages/cx_Freeze/initscripts/Console.py", line 26, in run
    exec(code, m.__dict__)
  File "vaas.py", line 2, in <module>
    import requests
  File "/usr/local/lib/python2.7/site-packages/requests/__init__.py", line 113, in <module>
    from . import packages
ImportError: cannot import name packages

我认为这可能是我的 cx_Freeze 设置或请求的问题,但由于我告诉 cx_Freeze 包含 'requests',这应该可行。 我检查了构建目录(cx_Freeze 构建请求模块已定义 'packages',我可以找到它:

[user@centos-vm]$ ls build/exe.linux-x86_64-2.7/lib/requests/packages/
chardet  idna  urllib3

非常感谢任何帮助!

尝试将 'request' 添加到 packages 列表选项,而不是将其添加到 includes 列表选项:

packages= ['urllib3', 'idna', 'requests']

根据 cx_Freeze documentationbuild_exe 选项 includes

comma separated list of names of modules to include

build_exe 选项 packages

comma separated list of packages to include, which includes all submodules in the package

找到解决此问题的方法:

编辑:

/usr/local/lib/python2.7/site-packages/requests/__init__.py

并注释掉:

from . import packages

确实是很奇怪的问题。我解冻的应用程序工作正常,但在使用 cx_freeze.

时需要上述解决方法