使用cx_Freeze和pyinstaller的ImportError

时间:2013-07-08 16:48:27

标签: python twisted pyinstaller cx-freeze

之前我使用的是pyinstaller尝试使用twisted作为可执行文件获取我的应用程序,但执行时出现此错误:

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.

那么,我尝试使用cx_freeze,但即使使用像this example.这样的'namespace_packages': ['zope']

,我也会遇到完全相同的错误

我正在构建可执行文件,我可以打开一个python解释器并成功导入zope.interface,然后我通过easy_install安装它,然后再运行pip install -U zope.interface,而不是有任何影响。

这是cx_freeze的setup.py

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")])

编辑1:忘记提及我还尝试在__init__.py下放置一个空白的zope.interface文件,这也没有帮助。

编辑2:当使用cx_freeze时,在build文件夹的library.zip中,zope.interface在那里,我认为没有任何模块丢失,但我仍然得到ImportError

这是来自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

编辑3:这是我的可执行文件的sys.path输出(缩写为..

['../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']

以下是我直接导入zope.interface时出现的错误:

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

在我的cx_freeze setup.py中将pkg_resources添加到我的包含后,程序已运行

4 个答案:

答案 0 :(得分:5)

pkg_resources添加到您的setup.py中的includes以获取cx_Freeze。

答案 1 :(得分:2)

我和cx_freeze有同样的问题。上述解决方案似乎都不适用于我的情况。 对我来说,here的解决方案起了作用:

你需要真正创造 zope/__init__.py作为空文件,以便正常处理 由imp.find_module()执行实际工作

答案 2 :(得分:0)

尝试添加build_exe_options子包的特定包含,即"includes": ["zope.interface"],这应该强制包含它。

答案 3 :(得分:0)

当我在include中添加'pkg_resources'并运行cx_freeze脚本时,我只得到前两行并保留在这里:

运行构建

运行build_exe