tags:

views:

851

answers:

3

Does anyone know how to associate the py extension with the python interpreter on Mac OS X 10.5.7? I have gotten as far as selecting the application with which to associate it (/System/Library/Frameworks/Python.framework/Versions/2.5/bin/python), but the python executable appears as a non-selectable grayed-out item. Any ideas?

Thanks, --Steve ([email protected])

+1  A: 

Steve, add the following to the top of your python script:

#!/usr/bin/env python

It must occur as the first line of the file.

Then make the file executable as so:

daves-macbookpro ~: chmod +x foo.py

Then all you need to do to run this is type

./foo.py
hobodave
Sure, this makes it executable from the terminal. I want to be able to double-click the python program and have it actually executable via the interpreter. I tried to (see my original post), but couldn't actually select the python interpreter as the application with which to launch the .py file.
Stephen Gross
+2  A: 

The file associations are done with the "Get Info". You select your .PY file, select the File menu; Get Info menu item.

Mid-way down the Get Info page is "Open With".

You can pick the Python Launcher. There's a Change All.. button that changes the association for all .py files.

S.Lott
+3  A: 

The python.org OS X Python installers include an application called "Python Launcher.app" which does exactly what you want. It gets installed into /Applications /Python n.n/ for n.n > 2.6 or /Applications/MacPython n.n/ for 2.5 and earlier. In its preference panel, you can specify which Python executable to launch; it can be any command-line path, including the Apple-installed one at /usr/bin/python2.5. You will also need to ensure that .py is associated with "Python Launcher"; you can use the Finder's Get Info command to do that as described elsewhere. Be aware, though, that this could be a security risk if downloaded .py scripts are automatically launched by your browser(s). (Note, the Apple-supplied Python in 10.5 does not include "Python Launcher.app").

Ned Deily
OK, this works. There are still two problems:(1) It won't open with the python launcher by default, even though I checked the "always open with this" in the preferences thingy.(2) When the python module terminates, the terminal problem doesn't quit. I want the terminal program to exit when the python executable finishes execution.Any ideas?
Stephen Gross
(1) Just to be clear, if you do a Finder Get Info on the .py script, the Info panel should show "Open with:" "Python Launcher (default)". If not, select "Python Launcher" in the Open With drop down. You should then be able to double-click on that script file, which should then cause "Python Launcher" to launch.(2) Probably the easiest way to get around that is to change Terminal.app preferences: Settings->Shell->When the shell exists:->Close if the shell exited cleanly.
Ned Deily
Another approach to this is to create a simple Applescript applet which contains a "do shell script" to run the Python script directly. Yet another approach is to use py2app to create a full-blown Python application bundle.
Ned Deily