简体   繁体   中英

ImportError: cannot import name packages

I am using cx_Freeze version 5.1.1 and I have a python 2.7.9 application to package using cx_Freeze. The application is using the python 'requests' module (the version of requests is 2.18.4)

Here is my cx_Freeze setup.py file (note I am specifically including 'requests' in the packages to include):

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
)

The build completed successfully creating the executable, but upon running my application I got the below error:

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

I am thinking it is either an issue with my cx_Freeze setup or with requests, but since I tell cx_Freeze to include 'requests', this should work. I checked in the build directory (that cx_Freeze builds that the requests modules has 'packages' defined and I can find it:

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

Any help is much appreciated!

Found a workaround to this issue:

edit:

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

and comment out:

from . import packages

Very strange issue indeed. My unfrozen application works just fine, but the above workaround is needed when using cx_freeze.

Try to add 'request' to the packages list option instead of adding it to the includes list options:

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

According to the cx_Freeze documentation , the build_exe option includes is a

comma separated list of names of modules to include

whereas the build_exe option packages is a

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM