So you want to use python on the mac?

by jesse in ,

4655664E-CBA5-4AF4-B813-87854FC67289.jpgIn a complete tangent from my numerous other projects, I've had a few people ask me recently about python on the mac, how to get started/etc. I'm going to solely focus on python in Leopard (10.5.x) and not anything before that. Anything before that is dead to me! DEAD!

If you open a shiny new macbook, and want to get started hacking, the first place you need to stop is the Apple Dev site, and download the latest version of XCode. XCode installs gcc and other tools which you are going to want and need sooner or later. It's a monster download, but bite the bullet.

By default, Leopard comes with Python 2.5.1 - the binaries and standard library (and any Leopard additions) are installed into /System/Library/Frameworks/Python.framework/Versions/2.5/ /Library/Python//, the site-packages directory is in /Library/Python/.../site-packages. This is known as a framework build of Python, and altering that installation is, well, a bad idea. I don't recommend it.

Now, some people may recommend you install macports or fink: these are both "sorta" package managers for OS/X, and while I do have macports installed, I do not use it for python work. I prefer compilation and self management, that and I've had issues with macports in the past. So, go ahead and install it, but don't use it for python work.

So, now you have xcode - fire up, from now on, this is your best friend. When you fire it up, you're in your /Users/username directory - your home directory. You want to look for one of the following files, .bashrc or .bash_profile. If the latter doesn't exist, do a "touch .bash_profile" - this will create an empty one for you, which you will need later on.

If you were to type "python" in right now, the default leopard install (2.5.1) would pop up. The site-packages directory lives in /Library/Python/2.5/site-packages. If you were to start installing python modules, that's where they would go. But we're not going to do that.

Head over to - go to the download page and download the latest 2.6 release of python (2.6.1 right now). Unpack and install it - the installer will install this into Library/Frameworks/Python.framework/ - this is outside of the default system install. It may install symbolic links to the binaries in /usr/local/bin, otherwise scripts that come with installed packages and the python binary itself go into /Library/Frameworks/Python.framework/Versions/2.6/bin.

The installer will also modify your ~/.bash_profile we created to prepend this onto your PATH variable. If you view your ~/.bash_profile you should see a modified version which adds the /usr/local/bin or the /Library/Frameworks/Python.framework/Versions/2.6/bin directory to your path (it should be labeled with a comment).

Note, that if the installer doesn't update your profile, simple add this line:

export PATH=/Library/Frameworks/Python.framework/Versions/2.6/bin:/usr/local/bin:$PATH

I totally boned this earlier: Just to clarify, the downloaded version of python installs symbolic links in /usr/local/bin which point to binaries in the /Library/Frameworks/Python.framework/Versions/2.6/bin - any other tools you install will place their binaries into that /Library/Frameworks/Python.framework/Versions/2.6/bin directory as well (some might also symlink into /usr/local/bin).

Do the following ". ~/.bash_profile" - this sources the profile and changes your environment. You only need to do this once for this window, any time you open a new terminal window, this is done for you.

Now you have the latest and greatest python 2.6 installed. You still have the system install in the default location, and you can swap between either one by using an explicit path, for example "/usr/bin/python" loads the system version.

IDLE is installed into the /Applications/Python... directory, if you need it.

So you have a new shiny install, and you're ready to get started...


If you started downloading libraries and stuff to this new install you'd be tainting an otherwise pristine installation of python. No, you wouldn't want to do that, would you?

Rather than taint it, we're going to install a few dependencies you'll need an a handy tool called virtualenv. Trust me, you'll thank me.

Most of the known python world uses easy_install (with setuptools) to install packages from the web. I prefer pip. To be effective in the world, you're going to want to install both, and only fall back to easy_install if pip fails you.

So, download this python script - save it to a temporary directory, your downloads folder, it doesn't matter. This is, and will bootstrap setuptools and easy_install for you - just run:

sudo python

Once that's complete, head over to pypi and download the pip tarball. Unpack it and execute "sudo python install".

Next up, well - let's cheat - in terminal, type:

sudo pip install virtualenv

This installs easy_setup and pip and virtualenv into the 2.6.1 install we installed previously. The binaries are installed to /Library/Frameworks/Python.framework/Versions/2.6/bin. Your egregious modification of this pristine install is almost complete.

Next install virtualenvwrapper:

sudo pip install virtualenvwrapper

The environment file is installed to /Library/Frameworks/Python.framework/Versions/2.6/bin/virtualenvwrapper_bashrc

Now edit your ~/.bash_profile and add two lines at the bottom:

export WORKON_HOME=$HOME/virtualenvs
source /Library/Frameworks/Python.framework/Versions/2.6/bin/virtualenvwrapper_bashrc

Save it and then run ". ~/.bash_profile" - this will add the virtualenv commands to your environment. Hooray! This also sets the default directory that they're stored in to "~/virtualenvs". Much victory to be had. You should run "mkdir ~/virtualenvs" at this point.

So, we have the basics. Now, you're going to want to make your default virtualenv, in terminal type:

mkvirtualenv default

This will create a ~/virtualenvs/default virtual environment where you can poke around and begin to install juicy things into. Note your prompt changes to reflect the environment loaded. I have my .bash_profile set to load the default version whenever I fire up, for example:

export WORKON_HOME=$HOME/virtualenvs
source $HOME/env_ext/virtualenvwrapper_bashrc
workon default

You can point editors/etc to point at this environment as well, binaries, libraries/etc are loaded into ~/virtualenvs/default/bin and ~/virtualenvs/default/lib for example.

You can install all sorts of goodies, for example, if I wanted to create a pygame environment, I would do this:

mkvirtualenv pygame
cd ~/virtualenvs/pygame
easy_install pygame

And then at any time, I could swap over to the pygame one with the "workon" command provided by virtualenv wrapper.

You can also create a virtual env pointed at the old system installation - something like this should do the trick:

cd ~/virtualenvs
virtualenv -p /usr/bin/python python2.5.1

And then "workon python2.5.1" will load the virtualenv pointing at that interpreter. Viola! You've managed to not taint the built in version, add a more recent version (modifying it only a little) and add the ability to rapidly swap environments. Hooray!

Astute readers might point out that adding the "to the side" install of python would remove access to the pyobjc bridge that ships with the built in version, you can pip/easy_install the objc libraries now: check out pypi. Also, adding the 2.5.1 virtualenv means you can hack on the shipped version of pyobjc as well.

I'd recommend ready the docs for virtualenv, virtualenv wrapper, pip and easy_install so you understand how things actually work, I've only covered the basics here.

The downloaded version of python doesn't taint your system, it installs cleanly to the side of the built in version of python, anything past that point is shell hackery.

Side note If you want a quick and dirty way of installing a more feature rich version of readline, you can also just run:

pip install readline

I'd also like to finally point out that this makes my life (I'm is running the default + latest shipped 2.6 + latest shipped 3.0 + 2 versions for python svn) life much easier to deal with.

See also this write up by Kumar McMillan