5

我有一个基于 Twisted 和 PyGTK 的 Python 应用程序。Twisted 本身依赖于 zope.interface,我不直接导入。

不幸的是,当我尝试运行我的应用程序时,错误日志中出现以下错误:

Traceback (most recent call last):
  File "tasks.py", line 4, in <module>
  File "ui\__init__.pyc", line 14, in <module>
  File "twisted\python\log.pyc", line 17, in <module>
ImportError: No module named zope.interface
Traceback (most recent call last):
  File "tasks.py", line 4, in <module>
  File "ui\__init__.pyc", line 14, in <module>
  File "twisted\python\log.pyc", line 17, in <module>
ImportError: No module named zope.interface
Traceback (most recent call last):
  File "tasks.py", line 4, in <module>
  File "ui\__init__.pyc", line 14, in <module>
  File "twisted\python\log.pyc", line 17, in <module>
ImportError: No module named zope.interface

我尝试将 and 的每个组合添加zope.interfacezopeand INCLUDESPACKAGES但这样做只会给我这个构建时间错误:

running py2exe
*** searching for required modules ***
C:\Python26\lib\site-packages\py2exe\build_exe.py:16: DeprecationWarning: the sets module is deprecated
  import sets
Traceback (most recent call last):
  File "setup.py", line 75, in <module>
    'gtk/*.ui'
  File "C:\Python26\lib\distutils\core.py", line 152, in setup
    dist.run_commands()
  File "C:\Python26\lib\distutils\dist.py", line 975, in run_commands
    self.run_command(cmd)
  File "C:\Python26\lib\distutils\dist.py", line 995, in run_command
    cmd_obj.run()
  File "C:\Python26\lib\site-packages\py2exe\build_exe.py", line 243, in run
    self._run()
  File "C:\Python26\lib\site-packages\py2exe\build_exe.py", line 296, in _run
    self.find_needed_modules(mf, required_files, required_modules)
  File "C:\Python26\lib\site-packages\py2exe\build_exe.py", line 1306, in find_needed_modules
    mf.import_hook(f)
  File "C:\Python26\lib\site-packages\py2exe\mf.py", line 719, in import_hook
    return Base.import_hook(self,name,caller,fromlist,level)
  File "C:\Python26\lib\site-packages\py2exe\mf.py", line 136, in import_hook
    q, tail = self.find_head_package(parent, name)
  File "C:\Python26\lib\site-packages\py2exe\mf.py", line 204, in find_head_package
    raise ImportError, "No module named " + qname
ImportError: No module named zope

setup.py的是:

from distutils.core import setup
import py2exe

def find_data_files(source,target,patterns):
    # I've elided this, I doubt it's relevant to the problem
    # ...

INCLUDES = [
    'cairo',
    'pango',
    'pangocairo',
    'atk',
    'gobject',
    'gio',
]

PACKAGES = [
    'encodings',
]

setup(
    name = 'MyApp',
    description = 'My Application',
    version = '1.0',

    windows = [
                  {
                      'script': os.path.join('ui','tasks.py'),
                      'icon_resources': [
                            (1, os.path.join(
                                'ui','data','iconpack.ico'))
                        ],
                  }
              ],

    options = {
                  'py2exe': {
                      'packages': ','.join(PACKAGES),
                      'includes': ','.join(INCLUDES),
                  }
              },

    data_files = find_data_files(
                    'ui', 'ui', [
                        'data/*',
                        'gtk/*.ui'
                    ])

)

如何让 py2exe 构建它?

4

3 回答 3

23

我不知道你是否解决了这个问题,或者它是否与你相关,但对于未来的搜索者,我在这里找到了一种解决 zope 导入问题的简单方法。

具体来说,将一个空__init__.py文件添加到PYTHONDIR/Lib/site-packages/zope目录中。

我用一个twisted应用程序测试了这个,使用这个设置文件:

#!/usr/bin/env python
'''
File: setup.py
Author: Spencer Rathbun
Date: 07/24/2012
Description: Setup file for py2exe, converts client python script into executable.
'''
from distutils.core import setup
import py2exe

INCLUDES = [
    ''
]

PACKAGES = [
    'twisted'
]

setup(
        name = 'client',
        description = '',
        version = '1.0',

        console = [
            {"script":"client.py",
                "icon_resources":[
                    (1, "c:\python27\DLLs\py.ico")
                    ]
                }
            ],
        zipfile = None,

        options = {"py2exe":
            {"compressed": 1,
                "optimize": 1,
                "ascii": 0,
                "bundle_files": 1,

                "packages": ','.join(PACKAGES),
                "includes": ','.join(INCLUDES),
                }
            }
        )

Py2exe 现在可以使用它成功地为 twisted 创建一个可执行文件,因为它依赖于 zope。

于 2012-07-24T13:39:39.793 回答
5

zope.interface和朋友(zope.component 等)也遇到过同样的问题。具体来说,如何搜索py2exe发现包以及如何安装包是一个问题。 zope

zope是一个命名空间包,因此依赖于它的.pth文件中的一些时髦的导入逻辑(请参阅 参考资料zope.interface-3.*.*-py2.*-nspkg.pth),以便将它的子包添加到 python 的路径中。看看它,site-packages你就会明白我的意思。

py2exe有问题“发现”这种包。

最后,我所做的是将zope我正在使用的各种软件包手动重新打包到一个标准模块设置中site-packages,然后重新运行py2exe——然后发现一切都没有问题。这是一个 PITA,但在py2exe能够处理包装边缘情况和/或zope包装以py2exe友好的方式包装之前,它是你能做的最好的。

于 2011-10-19T05:57:40.477 回答
3

我在 Windows XP SP3 中使用 py2exe 创建包时遇到了这个问题。我发现 py2exe 没有正确确定依赖关系。

为了解决这个问题,我卸载了我的第三方软件包并使用以下 easy_install 命令安装了它们

easy_install -Z <your_package_name>

-Z 选项解压缩包详细信息,因此内容不会被压缩。现在运行 py2exe 时,它​​会正确检测依赖项。

希望这可以帮助!

于 2013-12-03T19:06:21.517 回答