2

我有一个 Python 应用程序,它在运行时动态加载 python 模块(使用__import__)。要加载的模块位于一个名为“ plugins”的包中(即plugins__init__.pyetc 调用的子文件夹)。从 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夹中。现在插件找到了。但我仍然对这种丑陋的手动破解不满意。

4

1 回答 1

0

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] )
于 2011-01-07T17:11:56.863 回答