本文介绍了让 py2exe 与 zope.interface 一起工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a Python app based on Twisted and PyGTK. Twisted itself depends on zope.interface, and I don't import it directly.

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

Unfortunately, when I try to run my app, the following error ends up in the error log:

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

我已经尝试将 zope.interfacezope 的每个组合添加到 INCLUDESPACKAGES,但是这样做只会给我这个构建时间错误:

I've tried adding every combination of zope.interface and zope to INCLUDES and PACKAGES, but doing so only gives me this build time error:

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 来构建它?

How do I get py2exe to build this?

推荐答案

我和 zope.interface 和朋友(zope.component 等)遇到了同样的问题.具体来说,py2exe 搜索和发现包的方式 zope 包的安装方式存在问题.

I've had this same problem with zope.interface and friends (zope.component, et al). Specifically it is a problem with how py2exe searches and discovers packages AND how the zope packages are installed.

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

zope is a namespace package and as a result relies on some funky import logic in it's .pth files (see zope.interface-3.*.*-py2.*-nspkg.pth) in order to add it's sub-packages to python's path. Have a look at it in site-packages and you'll see what I mean.

py2exe 有问题发现"这种包.

py2exe has problems "discovering" this kind of package.

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

In the end what I did was manually repackage the various zope packages I was using into a stardard module setup in site-packages and then reran py2exe - which then discovered everything no problem. It's a PITA, but until py2exe is able to handle packaging edge cases and/or the zope packages are packaged in a py2exe friendly fashion, it's about the best you can do.

这篇关于让 py2exe 与 zope.interface 一起工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-15 23:25