How to setup correctly OSX for Python development

Let's setup OSX for Python development using homebrew and virtualenv.

Posted August 08th, 2012 in python, osx, homebrew

OSX is great for Python development, because it ships with the interpreter preinstalled. But sometimes you'll waste hours trying to make it work with your code.

So it's actually better to do not rely on what OSX gives you out of the box and install Python from scratch, but the easy way!

Prerequisites

  • a clean OSX installation (10.7 Lion or 10.8 Mountain Lion)
  • no /usr/local folder
  • Xcode installed
  • XQuartz for formulae that require X11 libraries. Thanks Rust to point it out on Hacker News.
  • an internet connection

The procedure is quite easy, but please consider that I will not be responsible for any loss or damage to your system that could result from following this how to.

Step 1: install homebrew

Homebrew is a package manager for OSX. Have you ever used apt-get/yum on Linux? Well, this is quite similar. By installing it you'll have access to a new command, brew, with which you'll be able to install, update, remove, search, etc., packages.

For a complete reference about installing options, refer to the official documentation.

Open a Terminal and paste this line:

ruby <(curl -fsSk https://raw.github.com/mxcl/homebrew/go)

You'll be guided through the installation process, so follow it carefully.

When it's done, you have brew installed but not ready to be used. First, do a sanity check of your environment:

brew doctor

It's very, very important that you solve all the problems it will bring up. If you have problems, try googling the error for insight or contact me.

Now that brew is ready, you can fetch the newest version of it and all formulae from GitHub.

brew update

Now you have to update your $PATH to give priority to the software installed by brew. Edit your ~/.bash_profile file and add the following at the beginning:

PATH=/usr/local/bin:$PATH

Congratulation! Your system is ready :)

Step 2: install Python

Now that we have brew installed and working, we can install Python.

brew install python

Homebrew will warn you that Python has been installed, but the default interpreter has not been touched. You can verify it typing:

$ which python
/usr/bin/python

Edit again your ~/.bash_profile file and change the line we wrote before like this:

PATH=/usr/local/bin:/usr/local/share/python:$PATH

Close your terminal, open it again and verify that the default Python interpreter now is the one installed by brew.

$ which python
/usr/local/bin/python

As a nice plus, you'll have pip available to install your tools.

Have fun!