Install Python 2.7.x under Snow Leopard / Lion (64 bits)

Mon, Feb 13, 2012 4-minute read

OS X comes with a Python installation that I find a bit confusing and misleading. What I present in this post is a guide to remove the default Python from your 64 bits OS X (i.e. Snow Leopard or Lion), install its latest version, and set up a nice environment to work with (e.g. iPython + MacVim). The only requirement for this process is to have the latest version of XCode, that you can get from the App Store.

Uninstall Python

I couldn’t find any nice and clean uninstaller for Python in OS X. So we will have to remove all files by hand. Here is the code:

sudo rm -rf /Library/Frameworks/Python.framework<br /> sudo rm -rf /Applications/Python*<br /> sudo rm -rf /Library/Python*<br /> cd /usr/local/bin<br /> sudo rm -rf 2to3 idle idle2.7 pydoc pydoc2.7 python* smtpd.py smtpd2.7.py<br /> cd /usr/bin<br /> sudo rm -rf 2to3 idle idle2.7 pydoc pydoc2.7 python* smtpd.py smtpd2.7.py<br />

(if you have another versions of Python installed (e.g. 2.6, 2.5), just change 2.7 for your version number when removing the files. However, that is not really that important, just so that you don’t have that much garbage in your computer).

Install Python 2.7.x

Now let’s install the latest release of 2.7.x for OS X. You can see the latest releases here. When writing this post, the latest version is 2.7.2.

Once you have run the installer, edit your ~/.bash_profile file. To edit your file, use your favorite text edito (vim in my case):

vim ~/.bash_profile

You should now have the following lines (add them if you don’t, but the installer is supposed to add them for you):

`

Setting PATH for Python 2.7

The orginal version is saved in .bash_profile.pysave

PATH=”/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}“ export PATH `

Now let’s add the compiler flags for 64-bits and the Python Path for the modules. This is important, otherwise your easy_install will probably fail. Add the following lines to your ~/.bash_profile:

export PYTHONPATH=/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages<br /> export CFLAGS="-I/Developer/SDKs/MacOSX10.7.sdk/usr/lib/"<br /> export ARCHFLAGS="-arch x86_64"

(note: change MacOSX10.7 to MacOSX10.6 if you’re using Snow Leopard in the second line)

Notice that all your modules will be installed here:
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages. It’s kind of important to remember this path, so you may want to add it to your sidebar in Finder or create some soft link.

Close your file and restart your bash so that the new environment variables are available. Check that you can now run python from the new terminal.

Install most common modules

I usually use easy_install to install new modules. You can get it from here (the module is called setuptools).

Download it, decompress it, and type the following from it’s folder:

python setup.py build<br /> python setup.py install

Now you can use easy_install to install your favorite modules. E.g.:

easy_install numpy<br /> easy_install scipy<br /> easy_install matplotlib<br /> easy_install ipython

If you’re running Lion, I highly recommend the SciPy SuperPack, which will do all the work for you.

Setting up your programming environment

This last section is completely biased due to my eternal love to VIM. I know some heartless people who use Emacs, or even Eclipse (I was one of them, and it was a terrible thing!) to code in Python.

If you’re a VIM lover you should already know about MacVim, but if you don’t you should definitively get it.

In any case, you should choose your favorite text editor (in case you want to use VIM -everybody wants to use VIM, but unfortunately some people don’t know that-, here it is a nice python module), and then I highly recommend using iPython instead of the classic Python console. This is especially useful for debugging and plotting. iPython is life changing, really.

To run iPython with matplotlib already running in the background just type:

ipython --pylab

Finally, here is a nice screenshot of how my programming environment looks like (iPython + PyLab + MacVim):

Beautiful isn’t it? : )

And finally, for something -not that- completely different: Happy programming!