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

3

5

So I have this pyqt project and I want to build a osx.app dmg using pyinstaller

pyinstaller created the output in

dist/MyApplication.app

I can run it directly from terminal

cd dist
./MyApplication.app/Contents/MacOS/MyApplication

However, if I try to run the app bundle directly either with

open -a MyApplication.app

or

open .
# double click on MyApplication.app folder (appears just as MyApplication from finder)

It starts and then quits immediately

Now if I navigate to

$ cd ./Contents/MacOS/

and open a finder

$ open .
#then double click on MyApplication

it runs fine, but with a terminal windows opened in the background

Last login: Fri Mar 14 18:01:13 on ttys005
MyApplication/dist/MyApplication.app/Contents/MacOS/MyApplication ; exit;
MyApplication/dist/MyApplication.app/Contents/MacOS/MyApplication ; exit;

I use similar steps to build a windows exe without any issues (although there is no MyApplication.app concept in windows)

How do I diagnose this issue?

Thanks

Schram answered 15/3, 2014 at 1:22 Comment(0)
S
2

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.

Schram answered 15/3, 2014 at 5:45 Comment(2)
I'm having this issue too. I don't think open -a is a valid solution (your app users will have to do it that way too).Wearproof
I'm running into the same issue. I know this post is kind of old, but if possible, can you elaborate just a bit on how you solved this? I'd greatly appreciate it.Human
T
3

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")

Trainload answered 24/1, 2017 at 23:36 Comment(0)
S
2

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.

Schram answered 15/3, 2014 at 5:45 Comment(2)
I'm having this issue too. I don't think open -a is a valid solution (your app users will have to do it that way too).Wearproof
I'm running into the same issue. I know this post is kind of old, but if possible, can you elaborate just a bit on how you solved this? I'd greatly appreciate it.Human
R
0

I had something very similar except I could run it with open -a AppName - it just crashed when run from finder.

In my case, it was an import involving ffmpeg which was the culprit:

from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip
Rodie answered 11/2, 2023 at 6:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.