views:

103

answers:

3

What's the best way to set up symbolic links to current installs, e.g python -> python2.6?

I've just installed python2.6 through Macports at /opt/local/bin/python2.6, I'd now like to set up a symbolic link called python here /usr/local/bin/. I then want to be able to add this line at the beginning of my pythons scripts so it knows where to look: #!/usr/local/bin/python. But what happens when I upgrade python to python2.7 for example, do I just need to remember to go to my symbolic link and change it? I guess I'll remember because it likely won't work anymore? Is there a better way to do this?

+1  A: 

Symlink the version you use most.

When you need another version, run it by specifying the version number, e.g.:

$ python2.5 dev_appserver.py myapp
Adam Bernier
A: 

Not sure about OSX, here is what I do on Ubuntu 9.04:

>which python
#/usr/bin/python

Just replace that file with a sym link to the version of Python you actually want to use:

>sudo ln -s /usr/bin/python2.6/python /usr/bin/python
Chase Seibert
No, you should *not* do this! /usr/bin/python is the Apple-supplied python. It's part of OS X: at worst, you risk breaking your system by doing that; at best, it will get overwritten by a future OS X update.
Ned Deily
+3  A: 

By default, MacPorts deliberately and carefully installs everything into a separate directory space: /opt/local. This ensures it does not conflict with anything installed as part of OS X or third-parties. To ensure that MacPorts-installed executables are found first, the recommended solution is to modify your shell PATH to put /opt/local/bin before /usr/bin.

MacPorts also provides a special port package, python_select, to manage which python version is pointed to by the command python in /opt/local/bin.

sudo port install python_select
sudo python_select

Then, to make your scripts use your current preferred python, the traditional solution is to use the env program in the shebang line of your scripts.

#!/usr/bin/env python
Ned Deily