Wednesday, December 21, 2011

Many Pythons on OSX (including Python 2.4)

If you need to install many pythons on OSX, this is the definitive resource:

http://jeetworks.org/node/101

The problem is that for Pythons <=2.6, you can’t easily build them from scratch with Apple’s SDK on 64bit machines. This process is an easy alternative to getting several self-contained Python installations on your machine including Python 2.4 (which probably isn’t installed by default on your machine).

Basically:


$ cd /opt
$ svn export http://svn.plone.org/svn/collective/buildout/python
$ cd python
$ /System/Library/Frameworks/Python.framework/Versions/2.5/bin/python bootstrap.py
$ bin/buildout

Then add the following to your .profile or whathaveyou and you can switch between them:


export ALT_PYTHON_24=/opt/python/python-2.4
export ALT_PYTHON_25=/opt/python/python-2.5
export ALT_PYTHON_26=/opt/python/python-2.6
export ALT_PYTHON_27=/opt/python/python-2.7
export ALT_PYTHON_32=/opt/python/python-3.2

strip_opt_python_from_path() { /usr/bin/python -c " import os import sys path = os.environ['PATH'] paths = path.split(os.path.pathsep) new_path = [] for p in paths: if not p.startswith('/opt/python') and p not in new_path: new_path.append(p) sys.stdout.write(os.path.pathsep.join(new_path)) " }

insert-opt-python-in-path() { if [[ -z $1 ]] then echo "Resetting to default native Python." export PATH="$(strip_opt_python_from_path)" else echo "Setting alternate Python: $1." export PATH="$1/bin:$(strip_opt_python_from_path)" fi echo "python is now: $(which python)." if ! type python3 >/dev/null 2>&1 then echo "python3 is not on path." else echo "python3 is now: $(which python3)." fi }

unimport-alt-python() { insert-opt-python-in-path }

import-alt-python-24() { insert-opt-python-in-path $ALT_PYTHON_24 }

import-alt-python-25() { insert-opt-python-in-path $ALT_PYTHON_25 }

import-alt-python-26() { insert-opt-python-in-path $ALT_PYTHON_26 }

import-alt-python-27() { insert-opt-python-in-path $ALT_PYTHON_27 }

import-alt-python-32() { insert-opt-python-in-path $ALT_PYTHON_32 }