Stephen Gross
Stephen Gross

Reputation: 5724

How to associate py extension with python launcher on Mac OS X?

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?

Upvotes: 5

Views: 15625

Answers (4)

cde
cde

Reputation: 327

The default python installation (atleast on 10.6.8) includes the Python Launcher.app in /System/Library/Frameworks/Python.framework/Resources/, which is aliased to the latest/current version of Python installed on the system. This application launches terminal and sets the right environment to run the script.

Upvotes: 0

Ned Deily
Ned Deily

Reputation: 85095

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

Upvotes: 6

S.Lott
S.Lott

Reputation: 391992

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.

Upvotes: 4

hobodave
hobodave

Reputation: 29301

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

Upvotes: 2

Related Questions