3

我有一个 pyobjc 应用程序在使用 gevent 库的仅 32 位 python 版本中运行。在 py2app 的别名模式下一切正常,但是一旦我构建了一个应用程序包,gevent 模块就找不到 httplib 库,即使它与 site-packages 目录捆绑在一起。

File "gevent/monkey.pyo", line 182, in patch_httplib
File "gevent/httplib.pyo", line 8, in <module>
ImportError: No module named httplib

我已经按照建议尝试了错误导入(即使模块似乎已被打包),但无济于事。它可以找到 gevent.httplib 模块,但找不到它应该修补的模块。这可能是猴子修补功能的问题吗?

编辑:看起来 find_module 无法与我的 py2app 包一起正常工作。有解决方法吗?我不认为点模块有问题,因为 httplib 没有点(它是核心 python 库的一部分)

编辑 2:所以它肯定是 imp.find_module。使用import ('httplib') 而不是 load_module 可以修复它,但我不得不删除 sys.modules 中对 'httplib' 的引用,因为如果它已经加载,它就无法修补。我认为这不是正确的方法,尽管构建的应用程序包可以正常工作(httplib 现在是猴子补丁并允许使用 HTTPSConnection 进行初始化)。这个py2app问题有什么解决方法/修复吗?

4

1 回答 1

2

这有点棘手,涉及更多补丁,但绝对可以解决:

def main():

    # Patch the imp standard library module to fix an incompatibility between
    # py2app and gevent.httplib while running a py2app build on Mac OS-X.
    # This patch must be executed before applying gevent's monkey patching.
    if getattr(sys, 'frozen', None) == 'macosx_app':

        import imp, httplib

        original_load_module = imp.load_module
        original_find_module = imp.find_module

        def custom_load_module(name, file, pathname, description):
            if name == '__httplib__':
                return httplib
            return original_load_module(name, file, pathname, description)

        def custom_find_module(name, path=None):
            if name == 'httplib':
                return (None, None, None)
            return original_find_module(name, path)

        imp.load_module = custom_load_module
        imp.find_module = custom_find_module

        # Verify that the patch is working properly (you can remove these lines safely)
        __httplib__ = imp.load_module('__httplib__', *imp.find_module('httplib'))
        assert __httplib__ is httplib

        # Your application here

if __name__ == '__main__':
    main()

This solution is more complex than simply modifying gevent.httplib, but at least works with the stock gevent 0.13 distribution. I haven't tried it with the recently released gevent 1.0 alpha versions yet.

于 2011-11-28T08:38:05.257 回答