Reputation: 1040
I'm trying to make a standalone, double click macosx app from a python module that has the
if __name__ == __main__
line in it to run. However, whenever I try to bundle it with something like py2app or pyinstaller, if I make an app bundle, a terminal window doesn't launch to run the program. Any idea on how to get around this? The terminal window needs to open in order to accept some user input. Let me know if more info is needed, this is the first time I've ever tried doing this.
P.S. I was able to get this to work for windows and package it within an installer for distribution.
Upvotes: 1
Views: 850
Reputation: 1040
Ok, I figured it out. Since building an app bundle from scratch is a bit tricky on a mac, it's better to find a program that will build it for you, I used applescript to accomplish this.
First, you need to build your executable file which is coming from the python code. I used pyinstaller with the --onefile option. so to build your executable use the command:
$pyinstaller --onefile yourPythonProgramYouWantToBuild.py
Once that's built, double click the file or open it with terminal to check that it works, all required code from various libraries have been included etc. If that works, onto the next step.
Open up applescript and set up a new script.
Here's how I got the thing to work 1) the applescript get's the directory its in 2) the compiled executable is going to be (manually) placed in the app bundle and the script inside the app is going to know where it is in the app bundle 3) the script finds the exact path to the executable, and tells the Terminal application to run it
Ok so how to do that?
Make a new applescript, and just put some dummy code in it, for example :
display dialog "Hello, world!"
Next, save this applescript as a app bundle. Go into the fresh app bundle you just made and navigate within the app's package contents Contents->Resources->Scripts->main.scpt
. Now open up the main.scpt file and begin to edit it.
In your main.scpt, first find the directory where the applescript is
tell application "Finder"
set current_path to container of (path to me) as string
end tell
However if you throw in a display dialog current_path
in the apple script, when you run that from the app bundle, you'll find that it won't take you all the way inside the app bundle. For example, if you were running the app from the desktop the path you would get would be something like ~/Desktop/
, which is not what we want, because we need to get the path inside the app. So next we need to append some stuff to the end of that in order to get to the good stuff inside the app bundle. Use a command similar to this:
set current_path to current_path & "TheNameOfThe.app" & ":" & "theNameOfYourCompiledExecutable"
Now we have a path to an executable that (might be) in your app's bundle. Now, make a POSIX path and tell terminal to run a the script.
set a to POSIX path of current_path
tell application "Terminal"
do script a
end tell
The last thing to do is to open up the app package contents and copy your onefile executable to the correct place within the app bundle. Change the app icon as necessary.
Upvotes: 1