Setting up your mac for Python scientific work

Alright, I've just received my new iMac at work. So, I've spent some decent amount of time setting Python and modules I commonly work with. Since, the process can be rather tedious and painful, I thought I'd put some details here. This post is about how to set up Python and some scientific python modules on your Mac. Here is a list of what we will install: python (2.7.3), numpy, scipy, matplotlib, pil, pyfits, lmfit, astropysics, pyephem, ipython, qtconsole and notebook.
When I had to do it myself, I found this link very useful (this post is basically a summary of that one, with some small variations). Also, here's a newer version of the same link.


So, you got your brand new shiny Mac (running Mountain Lion). The first thing you want to do is install homebrew, which simply is the least painful package manager that currently exists for Mac OS. You'll install it by opening up a terminal window and typing:

ruby -e "$(curl -fsSL"

You need this for all steps in this post, so if it doesn't work, you're screwed. Well, not entirely, you can look here for some help if it doesn't install. If it does install, you want to call the Doctor and check that everything is working before you install any package:

brew doctor

This will basically check that everything you need to install new packages is working fine. Do whatever the Doctor says. He or She will tell you things like "this program is outdated, install a newer version or make sure that this is on your PATH, ...". It doesn't always tell you how to solve the problem but gives you good clues.
Then, you should make sure the directory where homebrew installs program (/usr/local/bin/ if you followed standard homebrew installation) is on your path. Type echo $PATH in the terminal. If you don't see /usr/local/bin/, add a line in your ~/.profile or ~/.bash-profile (create the file if it does not exist). That line should be:

export PATH="/usr/local/bin":$PATH

If /usr/local/bin/ was on your path but after /usr/bin/, you should edit the files that set up the path in the first place. You can rearrange the lines of the /etc/paths. If that doesn't work, check this post.
We will be installing various programs that are currently not in the default brew distribution (tap). So, "tap" the following distributions:

brew tap home-brew/science
brew tap samueljohn/python


Alright, you have brew working and your path is set, now let's start installing things. Lots of the software we will install work best you re-install python using homebrew. Yes, I know. You already have python, why would you install it again? To avoid pain (check here for actual reasons and details). To install Python (2.7.3 at the time I wrote this post), type in a new terminal window (to refresh the path):

brew install python --framework --universal

Again, make sure /usr/local/share/python is in your path and add it if not (export PATH=/usr/local/share/python:$PATH). Reload Terminal after any change. After installing Python, you should tell your system that the newly installed version is the one that you want to use by default. To do that, we will create a symbolic link to the desired version of Python:

cd /System/Library/Frameworks/Python.framework/Versions
sudo rm Current
ln -s /usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/Current

If it works, typing which python should return /usr/local/bin/python. You might have to restart your terminal. Furthermore, if you type python, you should see "Python 2.7.3".

Python modules

Install these. One at a time. Note that I had a few problems while installing numpy and PIL because of conflicting older files. brew told me what to do though. I had to link numpy and PIL manually (brew link numpy --overwrite), which overwrote the conflicting files.

brew install numpy # Installs numpy
brew install gfortran # Fortran compiler for scipy
brew install scipy # Installs scipy
brew install matplotlib # Installs matplotlib
brew install pil # Installs PIL
pip install pyfits # Installs pyfits
pip install astropysics # Installs astropysics
pip install pyephem # Installs pyephem
pip install lmfit # Installs lmfit

IPython, QTConsole and notebooks

pip install ipython # Installs IPython
brew install pyqt # Installs pyqt
brew install zmq
pip install pyzmq
pip install pygments

You should now be able to launch the qtconsole by executing:

ipython qtconsole --pylab=inline

For installing notebook, see this link.


Popular Posts