2
我正在Python 2上使用py2app,並想知道是否有方法從代碼中檢測我的應用程序是作爲已編譯的.app文件的一部分運行還是作爲獨立腳本運行。Python:確定是否在.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文件夾)
幫助讚賞!