gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 06:39:14 25 4
gpt4 key购买 nike

我有一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com