farmdev

Python 3.0 On Mac OS X (alongside 2.6, 2.5, etc)

Python 3.0 is out. Woo! Some people have been saying that they'd like to be able to run it alongside 2.6, 2.5, etc to test it out. Well, Python is actually designed for this. All you have to do is build it with make altinstall and that will give you a python3.0 binary without touching your default python binary. But on Mac OS X it's a little different.

First, there isn't a DMG installer available yet but there probably will be one soon. UPDATE: as of 3.0.1 a Mac OS X installer is available. However, building a Framework from source is pretty simple. You will need Apple Developer tools installed to get a C compiler. Then just download the Python source from above, and install on Mac OS X like:

$ cd ~/Downloads/Python-3.0/
$ ./configure --enable-framework MACOSX_DEPLOYMENT_TARGET=10.5 --with-universal-archs=all
$ export LC_CTYPE=en_US.UTF-8
$ make && make test

(UPDATE: see comments for some notes about this configure command, YMMV. Also, it is a little trickier to get readline to work, i.e. for command history in the interpreter. Chris Miles explains how to do this.)

After the tests pass, install with:

$ sudo make frameworkinstall

But ... you're not done. There isn't an altframeworkinstall target so you will be overwriting your default python binary. The fix for this is easy:

$ cd /Library/Frameworks/Python.framework/Versions/
$ ls -l
total 8
drwxrwxr-x  10 root  admin  340 Nov 10 14:47 2.4
drwxrwxr-x  10 root  admin  340 Feb 22  2008 2.5
drwxrwxr-x  10 root  admin  340 Oct  1 18:52 2.6
drwxr-xr-x  10 root  admin  340 Dec  5 11:33 3.0
lrwxr-xr-x   1 root  admin    3 Dec  5 11:34 Current -> 3.0
$ sudo rm Current
$ sudo ln -s 2.5 Current
$ ls -l
total 8
drwxrwxr-x  10 root  admin  340 Nov 10 14:47 2.4
drwxrwxr-x  10 root  admin  340 Feb 22  2008 2.5
drwxrwxr-x  10 root  admin  340 Oct  1 18:52 2.6
drwxr-xr-x  10 root  admin  340 Dec  5 11:33 3.0
lrwxr-xr-x   1 root  admin    3 Dec  5 11:34 Current -> 2.5

NOTE: If you haven't ever installed Python yourself then you might be using the default Apple Python which for some crazy reason is located in /System/Library/Frameworks/Python.framework/Versions. Be sure to adjust the Current symlink appropriately.

Open a new shell and type python -V. It should be 2.5 or whatever you set it to. Phew! The only other step is to modify your .bash_profile or the equivalent for the new bin path. Be sure it does not override your other Python paths. Mine looks like this (note: 2.5 is Current):

PATH="/Library/Frameworks/Python.framework/Versions/3.0/bin:${PATH}"
PATH="/Library/Frameworks/Python.framework/Versions/2.4/bin:${PATH}"
PATH="/Library/Frameworks/Python.framework/Versions/2.6/bin:${PATH}"
PATH="/Library/Frameworks/Python.framework/Versions/Current/bin:${PATH}"
export PATH

Now you can type python3.0 and you'll be in a 3.0 shell. What do you next? Hrm, there's not much to do in 3.0 yet. You could experiment with the 2to3 script on some of your favorite modules :)