Jan 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. :)

Jan 2, 2010

New Year's Evolutions

Happy New Year!  Welcome to 2010!

Like many others, for a very long time, I was a New-Year's-Resolutions-maker.  I would sit down, think seriously for some time, then write a list of the things I was going to accomplish in the coming year.  I would even craft elaborate plans about how I was going to achieve each item on my list.

However, as the year progressed, I would lose focus or interest and many of my resolutions would become distant memories until the next new year. :(

I've decided to break this cycle! From now on, I make New Year's "Evolutions"!

Why?  Because every success I've had in my life has come as a result of being willing to change!

For my New Year's Evolutions:

  • I will evolve to reach the end goals
  • Evolving takes an indeterminate amount time, so I will not feel pressure to complete evolutions within a given time frame.  They will be done when they are done
  • Evolution usually leads to something better so any progress I make will be a positive change in my life
  • Evolution is natural so the changes I need to make to fulfill an evolution should also feel natural
  • I will get the help of as many people as necessary to achieve each evolution
  • I will evolve faster by being willing to make changes to changes I've already made
  • ... you get the point
I've always hated making a New Year's list.... until now. ;)