python - py2app:如何包含将由 __import__ 加载的模块?

标签 python macos py2app

我有一个 Python 应用程序,它在运行时动态加载 python 模块(使用 __import__)。要加载的模块位于名为“plugins”的包中(即名为 plugins 的子文件夹,其中包含 __init__.py 等)。从 python 解释器运行一切正常,甚至在使用 py2exe 编译为 Windows 二进制文件时也是如此。

我尝试从它构建一个 OSX 应用程序,它成功了,但是在运行 .app 时我得到一个 ImportError:'no module named plugins.xxxx'。

我很确定我为 py2app 提供了正确的选项('includes': [...]'packages':['plugins'],完全相同到我为 py2exe 所做的),因为当我浏览 .app 内容时,我看到文件夹 Contents/Resources/lib/python2.5/plugins/ 中的所有插件模块。

那么,为什么应用程序找不到我的模块(一定是路径问题)?

编辑:

我找到了一种方法让它工作,但这不是一个好的解决方案: 当我打印 Python 搜索模块的路径时(使用 print sys.path),我注意到文件夹 Contents/Resources/lib/python2.5/plugins/ 不是上市。但是,文件夹Contents/Resources/ 是,所以我将plugins 文件夹移动到Contents/Resources 文件夹。现在插件找到了。但我仍然对这种丑陋的手动 hack 不满意。

最佳答案

Koo 使用以下代码。您可能需要做一些类似的事情来检测您是否在 py2app 中,并相应地调整您的导入。

http://bazaar.launchpad.net/~openobject-client-kde/openobject-client-kde/5.0/annotate/head%3A/Koo/Common/Plugins.py

def scan( module, directory ):
        pluginImports = __import__(module, globals(), locals())
        # Check if it's being run using py2exe or py2app environment
        frozen = getattr(sys, 'frozen', None)
        if frozen == 'macosx_app' or hasattr(pluginImports, '__loader__'):
                # If it's run using py2exe or py2app environment, all files will be in a single 
                # zip file and we can't use listdir() to find all available plugins.
                zipFiles = pluginImports.__loader__._files
                moduleDir = os.sep.join( module.split('.') )
                files = [zipFiles[file][0] for file in zipFiles.keys() if moduleDir in file]
                files = [file for file in files if '__init__.py' in file]
                for file in files:
                        d = os.path.dirname(file)
                        if d.endswith( moduleDir ):
                                continue
                        newModule = os.path.basename(os.path.dirname(file))
                        __import__( '%s.%s' % (module, newModule), globals(), locals(), [newModule] )
        else:
                for i in os.listdir(directory):
                        path = os.path.join( directory, i, '__init__.py' )
                        if os.path.isfile( path ):
                                __import__( '%s.%s' % (module, i), globals(), locals(), [i] )

关于python - py2app:如何包含将由 __import__ 加载的模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3525944/

相关文章:

python - 按公共(public)列合并两个表

javascript - keydown 上普通字符和半字符的区别

python - Py2app 模块图中出现错误

macos - 有没有人在 Snow Leopard 中成功构建过 PyObjC 应用程序?

python - 使用 Py2app 时出现导入错误

python - 如何使用win32com处理查询桌面搜索时溢出?

python - 加载代码的 require 与 import 方法的优缺点是什么?

python - BeautifulSoup get_text 不会去除所有标签和 JavaScript

python - iokit.IOServiceGetMatchingServices 在 Python3 下损坏?

xcode freetype ld 符号未找到架构 x86_64