OSX app built with python output right away if app bundle is run from finder but works fine from command line

So I have this pyqt project and I want to create an osx.app dmg using pyinstaller

pyinstaller created 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 or with

open -a MyApplication.app

      

or

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

      

It starts and then exits immediately

Now if I go to

$ cd ./Contents/MacOS/

      

and open the search engine

$ open .
#then double click on MyApplication

      

it works fine, but with terminal windows open in 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 am using similar steps to create windows exe without any problem (although there is no concept of MyApplication.app on Windows)

How do I diagnose this problem?

thank

+5


source to share


2 answers


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

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

      

and got similar results

i.e. the app closes when i do

open -a HelloTest.app

      

so far it works 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 looks like if I run

open -a MyApplication.app

      

the application runs in a sandbox, and if you open any file for writing without specifying an absolute path, it will not be able to create the file

if i run

./MyApplication.app/Contents/MacOS/MyApplication

      

directly the application can create files in the current directory

So I need to go back and give the full path when creating the files, not just assume it will be created in the working directory.

+2


source


When you build an OSX app package using pyinstaller it will crash if you use relative paths and try to write files to the working directory.

To write files to the working directory without using absolute paths, create your paths like this:



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

+1


source







All Articles