Thursday, January 7, 2010

How to change the default Python version on Snow Leopard

Premise:
  • I have a MacBook Pro (Leopard) that originally came with Python 2.3
  • I upgraded to Python 2.5 (required for projects I work on)
  • All was well for quite some time until...
  • I needed to upgrade the OS from Leopard to Snow Leopard
  • Snow Leopard installs Python 2.6, but I still need Python 2.5 (and my installed modules)
  • What to do?
Post Snow Leopard upgrade:

After the upgrade I found that my current Python version was indeed 2.6 as I expected, and was surprised (and glad) to find that my original 2.3 and 2.5 versions were still intact:
bkelsie$ ls -Al /System/Library/Frameworks/Python.framework/Versions/
drwxr-xr-x   8 root  wheel  272 28 Dec 11:23 2.3
drwxr-xr-x  11 root  wheel  374 28 Dec 11:23 2.5
drwxr-xr-x  11 root  wheel  374 28 Dec 11:23 2.6
lrwxr-xr-x   1 root  wheel    3 28 Dec 11:23 Current > 2.6


I also found that the Python 2.5 modules I installed were also intact:
bkelsie$ ls -Al /Library/Python/2.5/site-packages/ | wc -l
      12


How to change the default Python version:

It turns out that in Snow Leopard, "python" is not a symbolic link to the current version.  It is a selector!
bkelsie$ which python
/usr/bin/python

bkelsie$ ls -lA /usr/bin/python*
-rwxr-xr-x  2 root  wheel  86000  7 Jul  2009 /usr/bin/python
-rwxr-xr-x  5 root  wheel    925  7 Jul  2009 /usr/bin/python-config
lrwxr-xr-x  1 root  wheel     75 28 Dec 11:23 /usr/bin/python2.5 -> ../../System/Library/Frameworks/Python.framework/Versions/2.5/bin/python2.5
lrwxr-xr-x  1 root  wheel     82 28 Dec 11:23 /usr/bin/python2.5-config -> ../../System/Library/Frameworks/Python.framework/Versions/2.5/bin/python2.5-config
lrwxr-xr-x  1 root  wheel     75 28 Dec 11:23 /usr/bin/python2.6 -> ../../System/Library/Frameworks/Python.framework/Versions/2.6/bin/python2.6
lrwxr-xr-x  1 root  wheel     82 28 Dec 11:23 /usr/bin/python2.6-config -> ../../System/Library/Frameworks/Python.framework/Versions/2.6/bin/python2.6-config
-rwxr-xr-x  2 root  wheel  86000  7 Jul  2009 /usr/bin/pythonw
lrwxr-xr-x  1 root  wheel     76 28 Dec 11:23 /usr/bin/pythonw2.5 -> ../../System/Library/Frameworks/Python.framework/Versions/2.5/bin/pythonw2.5
lrwxr-xr-x  1 root  wheel     76 28 Dec 11:23 /usr/bin/pythonw2.6 -> ../../System/Library/Frameworks/Python.framework/Versions/2.6/bin/pythonw2.6


A quick read of the python MAN page helped me figure out how to change the current version:
bkelsie$ man python
 

 PYTHON(1)                 BSD General Commands Manual                PYTHON(1)

NAME
     python, pythonw -- an interpreted, interactive, object-oriented programming language

SYNOPSIS
     python ...
     pythonw ...

DESCRIPTION
     To support multiple versions, the programs named python and pythonw now just select the real version of Python to run, depending on various settings.  (As of Python 2.5, python and pythonw are interchangeable; both execute Python in the context of an application bundle, which means they have access to the Graphical User Interface; thus both can, when properly programmed, display windows, dialogs, etc.)  The current supported versions are 2.5 (provided for backward-compatibility with the Python 2.5 family), 2.6 and 3.0, with the default being 2.6.
     Use
           % man python2.5
           % man python2.6
           % man python3.0
           % man pythonw2.5
           % man pythonw2.6
           % man pythonw3.0

     to see the man page for a specific version.  Without a version specified,
           % man pydoc

     and the like, will show the man page for the (unmodified) default version of Python (2.6).  To see the man page for a specific version, use, for example,
           % man pydoc2.5

CHANGING THE DEFAULT PYTHON
     Using
           % defaults write com.apple.versioner.python Version 2.5

     will make version 2.5 the user default when running the both the python and pythonw commands (versioner is the internal name of the version-selection software used).

...

So it was as simple as telling the OS which version to use:
bkelsie$ python --version
Python 2.6.1

bkelsie$ defaults write com.apple.versioner.python Version 2.5
bkelsie$ python --version
Python 2.5.4

bkelsie$ defaults write com.apple.versioner.python Version 2.6
bkelsie$ python --version
Python 2.6.1

 Trying to set the version to one that is not installed results in the default being chosen:
bkelsie$ defaults write com.apple.versioner.python Version 3.0
bkelsie$ python --version
Python 2.6.1

So now whenever I want to change my current Python version I just run the handy little one-liner!

I hope this was helpful. :)

0 comments:

Post a Comment