Multiple versions of Python on Mac with Macports

Python on Mac!Linux users would be familiar with “aptitude” as our virtual swiss army knife to install useful open source packages and software.

The bash command:

sudo aptitude install [package name]

gets us what we want most of the time.  Some less-common software library requires more tweaking and our manual intervention to make and build of course, but in general “aptitude” suffices.

MacPorts

On Mac OS, “port” is our new best friend.  You can find out all about the MacPorts project over on www.macports.org.

sudo port install [package name]

does most of our heavy lifting whenever we need to install 3rd party, open source libraries.

After we have downloaded the appropriate dmg file and installed the appropriate version (it has different versions for Snow Leopard, Leopard and Tiger), we should make sure that we have the latest available release by running:

sudo port -v selfupdate

Python

My Mac Snow Leopard comes pre-installed with the the Apple Darwin‘s version of Python 2.6.1, which is a little outdated for my taste.

Since I already have MacPorts installed, I can now get up-to-speed with the latest version of Python by running:

sudo port install python26
sudo port install python27
sudo port install python_select
sudo python_select python26

To view the list of different Python versions available on my Mac, I can simply type:

python_select -l

Because I have also installed Python 3.1 to check out the coolest features in Python 3 while I still work with a lot of legacy projects running Python 2.6, here are the results from my terminal when I run “python_select -l”:

calvin$ python_select -l
Available versions:
current none python24 python25 python26 python26-apple python27 python31

iPython

Since I can’t stand working with the plain Python shell, my modus operandi would be also to install iPython for Python 2.6, like this:

sudo port -v install py26-ipython

The command for running this iPython shell will be located in /opt/local/bin/ipython2-6.

UPDATES: Useful Commands

So a friend who’s new to Python and learning all about Python and Django emailed me to ask me some questions.  In response, I have listed out some commands which I think would be useful to him.

which python       # Shows us the path to our Python program
python --version  # Tells us which version of Python we are currently using
python_select -l   # Lists out the different versions of Python we have in our OS
sudo python_select python26      # Selects Python 2.6 for current use!

If you have any other interesting and useful commands to share, pray tell.

  • Kimmo Sundqvist

    What does “current use” mean? If something in the system software needs the Apple’s Python 2.6 then will it be able to find it, no matter which Python you set as current using python_select?

    I have Gallery installed via Macports and it wanted Python 2.7.2 with it. Will Gallery still work even if I set the current Python to something else?

  • Anonymous

    Just to note that python_select has been depreciated in the most recent version of MacPorts so if you are having problems with switching python versions try this:

    to list what versions of python are installed:
        sudo port select –list python

    examples to switch to a specific version of python:
        sudo port select –set python python27
        sudo port select –set python python26-apple

    intvoid.com

  • http://www.calvinx.com Calvin Cheng

    @brentvoid:disqus : thank you for the update. you are absolutely right that the latest version of MacPorts is now using port select.