I'd like to write cross platform Python scripts that are GUI frontends for command line programs. The problem is I know a few Mac users who think that using the terminal will have the same effect as throwing their computer off the top of a skyscraper. In Linux and Windows it's easy enough to setup a Python script so the user can double click an icon and the script will start without opening any extra windows. Is there an easy way to do this with OS-X? Would the user have to install a different Python than the one that comes with OS-X? I haven't been able to find a definitive answer.
-
1What have you tried? Do you have a working icon somewhere on the Mac that does something similar to what you want? – John Zwinck Feb 10 '13 at 00:51
3 Answers
You might want to look at Platypus. It's a freeware app for generating apps which wrap scripts.
Another way to do something like that is using Automator or even AppleScript Editor. Either can produce an application which just runs a script.
Update:
For Automator: Launch Automator, select the Application template, type "script" in the search field, double-click Run Shell Script, switch the shell pop-up menu to /usr/bin/python
, type/paste your Python script into the text field. Or, leave the pop-menu on /bin/bash
and just write an invocation of an external script in the text field. Save as an application.
You can also view help from its Help menu.
For AppleScript, launch AppleScript Editor, type the following as the script:
do shell script "/usr/bin/true"
Replace /usr/bin/true
with the path to whatever script you like. Save as an application.
Again, there's help in the Help menu.

- 88,520
- 7
- 116
- 154
-
So the only way is to create an app? Where can I find out about using Automator and/or AppleScript Editor with Python? – Dave Brunker Feb 10 '13 at 17:07
py2app does this with aplomb. You make your Python script, use whatever dependencies you need (wx, Tkinter, etc.) and py2app makes you a standalone app bundle that will run in any modern OS X environment. It bundles Python too, so you can use any Python you want (not just the system default).
The downside is that the generated apps might be large, up to 50MB if you have a lot of dependencies (though that is somewhat of an extreme).

- 171,345
- 36
- 312
- 383
-
Can the Mac have more than one Python installed at the same time, like the default and Active State? – Dave Brunker Feb 10 '13 at 17:01
-
Yep. As long as the copies are installed to different places (e.g. /System/Library, /Library, /usr), or are different Python versions, they can coexist happily. – nneonneo Feb 10 '13 at 17:08
There are two ways to do this:
- Click on a script.
- Press command-i to open the "get info" window.
- Expand the "Open With" section (if it isn't already).
- Choose "Python Launcher" from the drop-down menu.
- Click "Change All" if you would like ALL Python scripts to launch when double clicked.
- Possibly open Python Launcher and uncheck "Run in a Terminal window" This will work for this machine only, so it is less portable than the following. Why? Because the default for opening a document type varies depending on what is installed (XCode and/or IDLE will both take over opening a .py file).
Method Two:
- Validate the Interpreter Directive, that's the first line of the file. I suggest using
/usr/bin/env python3
. This will run the first python3 interpreter that is on the users path. - Make the script executable
chmod a+x <script_name>
from the Terminal. - Change the extension from .py to .command (this will be opened by the Terminal).
- Use zip or tar for distribution so that the permissions do not get mangled. This method will open a Terminal window, but when the Python window is closed the terminal window will also close.
If your script has dependencies outside of the standard library, then you should provide a second .command
file to install those. This may make things more complicated, but using pip3 install --user <list of dependencies>
should minimize complications.

- 21
- 2