我在 Python 2 上使用 py2app,想知道是否有办法从代码中检测我的应用程序是作为已编译的 .app 文件的一部分运行,还是作为独立脚本运行。
这很重要,因为我的一些代码会动态加载 .dylib 文件。当作为脚本本机运行时,此代码运行良好;但是,当在 .app 中运行时,我得到如下信息:
Traceback (most recent call last):
File ".../python/game/dist/main.app/Contents/Resources/__boot__.py", line 316, in <module>
_run()
File ".../python/game/dist/main.app/Contents/Resources/__boot__.py", line 311, in _run
exec(compile(source, path, 'exec'), globals(), globals())
File ".../python/game/dist/main.app/Contents/Resources/main.py", line 1, in <module>
import game
File "game.pyc", line 10, in <module>
File "gui.pyc", line 5, in <module>
File "audio/__init__.pyc", line 2, in <module>
File "audio/pybass.pyc", line 65, in <module>
File "ctypes/__init__.pyc", line 365, in __init__
OSError: dlopen(...python/game/dist/main.app/Contents/Resources/lib/python2.7/site-packages.zip/audio/libbass.dylib, 6): no suitable image found. Did find:
.../python/game/dist/main.app/Contents/Resources/lib/python2.7/site-packages.zip/audio/libbass.dylib: stat() failed with errno=20
2013-07-15 02:50:21.146 main[10040:707] main Error
我认为最好的方法是将 .dylib 文件移出库并进入资源,然后从那里动态加载它 - 但为了做到这一点,我需要在代码中检测我是否正在运行.app 与否,如果是,请在不同的位置查找库(即,应用程序包的 Resources 文件夹)
帮助表示赞赏!