OSX app built with python quits immediately if app bundle is executed from finder but runs fine from command line

走远了吗. 提交于 2019-12-05 12:28:37

So I followed this py2app tutorial to see if it works better than pyinstaller, with this code

if __name__=="__main__":
    print "Hello"

and got similar results

i.e. app closes when I do

open -a HelloTest.app

while it runs fine with

./HelloTest.app/Contents/MacOS/HelloTest

but then this tidbit in the tutorial explains it

When run normally, your application’s stdout 
and stderr output will go to the Console logs. 
To see them, open the Console application:

$ open -a Console

After examining the console logs, it seems like if I run

open -a MyApplication.app

the app runs in a sandbox and if you open any file to write without specifying absolute path it will fail to create the file

if I run

./MyApplication.app/Contents/MacOS/MyApplication

directly the app can create files in the current directory

So I have to go back and specify the full path while creating files, instead of just assuming it will create in the working directory.

When you build an OSX app bundle with pyinstaller, it will crash if you use relative paths and try to write files in the working directory.

To write files in the working directory without using absolute paths, make your paths like this:

path = os.path.join(os.path.dirname(sys.argv[0]), "file")

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!