ImportError with cx_Freeze and pyinstaller

2019-02-10 17:03发布

问题:

I was using pyinstaller before to try and get my app with twisted as an executable, but I got this error when executing:

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/cx_Freeze/initscripts/Console.py", line 27, in <module>
    exec code in m.__dict__
  File "client_test.py", line 2, in <module>
  File "/usr/local/lib/python2.7/dist-packages/Twisted-13.0.0-py2.7-linux-x86_64.egg/twisted/__init__.py", line 53, in <module>
    _checkRequirements()
  File "/usr/local/lib/python2.7/dist-packages/Twisted-13.0.0-py2.7-linux-x86_64.egg/twisted/__init__.py", line 37, in _checkRequirements
    raise ImportError(required + ": no module named zope.interface.")
ImportError: Twisted requires zope.interface 3.6.0 or later: no module named zope.interface.

So then, I tried using cx_freeze, but I get the exact same error, even when using 'namespace_packages': ['zope'] like this example.

From where I'm building the executable, I can open a python interpreter and sucessfully import zope.interface, and I installed it through easy_install, then ran pip install -U zope.interface later on, which didn't have any effect.

Here's my setup.py for cx_freeze:

import sys
from cx_Freeze import setup, Executable

# Dependencies are automatically detected, but it might need fine tuning.
build_exe_options = {"excludes": ["tkinter"],
             'namespace_packages':['zope'],
            'append_script_to_exe':True
}

setup(  name = "exetest",
        version = "0.1",
        description = "My first executable",
        options = {"build_exe": build_exe_options},
        executables = [Executable("client_test.py")])

EDIT 1: Forgot to mention that I also tried putting a blank __init__.py file under zope.interface, and that also didn't help.

EDIT 2: When using cx_freeze, inside the library.zip of the build folder, zope.interface is in there and I don't think any of the modules are missing, but I still get the ImportError

This is from the output of cx_freeze:

Missing modules:
? _md5 imported from hashlib
? _sha imported from hashlib
? _sha256 imported from hashlib
? _sha512 imported from hashlib
? builtins imported from zope.schema._compat
? ctypes.macholib.dyld imported from ctypes.util
? dl imported from OpenSSL
? html imported from twisted.web.server
? netbios imported from uuid
? ordereddict imported from zope.schema._compat
? queue imported from twisted.internet.threads
? twisted.python._epoll imported from twisted.internet.epollreactor
? twisted.python._initgroups imported from twisted.python.util
? urllib.parse imported from twisted.web.server
? win32wnet imported from uuid
? wsaccel.utf8validator imported from autobahn.utf8validator
? zope.i18nmessageid imported from zope.schema._messageid
? zope.testing.cleanup imported from zope.schema.vocabulary

EDIT 3: Here's the sys.path output from my executable (shortened with the ..)

['../build/exe.linux-x86_64-2.7/client_test',
 '../build/exe.linux-x86_64-2.7',
 '../build/exe.linux-x86_64-2.7/client_test.zip',
 '../build/exe.linux-x86_64-2.7/library.zip']

Here's the error I get when I import zope.interface directly:

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/cx_Freeze/initscripts/Console.py", line 27, in <module>
    exec code in m.__dict__
  File "client_test.py", line 3, in <module>
  File "/usr/local/lib/python2.7/dist-packages/zope.schema-4.3.2-py2.7.egg/zope/__init__.py", line 1, in <module>
    __import__('pkg_resources').declare_namespace(__name__)
ImportError: No module named pkg_resources

After adding pkg_resources to my includes in my cx_freeze setup.py, the program ran

回答1:

Add pkg_resources to your includes in your setup.py for cx_Freeze.



回答2:

I had the same issue with cx_freeze. None of the above solutions seemed to work in my case. For me this solution from here worked :

You need to actually create zope/__init__.py as an empty file so that the normal processing performed by imp.find_module() actually works



回答3:

Try adding to build_exe_options a specific include of the subpackage i.e. "includes": ["zope.interface"], this should force the inclusion of it.



回答4:

When I add 'pkg_resources' in includes and run the cx_freeze script, I only get these first two lines and it stays here :

running build

running build_exe