Header image  
Theoretical Astrophysics  
 
    home

Setting up Python and Matplotlib in OSX Mountain Lion

After setting up a few new Macs for use with python and matplotlib I thought it would be good to write down a few notes about how I set it up. This procedure seems to work very well for a clean install of OSX Mountain Lion, and I think it worked well with Lion as well. OSX comes with a python distribution, which as of this writing was 2.7.3 however we want to set up several tools and packages that require some work.

Step 1: Xcode and command line tools

Xcode is the developer's suite for OSX that comes free from Apple, however, it doesn't come installed by default. You can install Xcode via the App Store. Since OSX Lion, the Xcode installation doesn't by default include the command line tools, which include things we will need, in particular a script called easy_install which we will use shortly to bootstrap our installation of virtualenv.

Once Xcode has been installed, run Xcode and then open the preferences. Select the Download section and the Components tab and install Command Line Tools from there.

where to find command line tools

Step 2: virtualenv

For this and the next few parts I'm following Michele's excellent compusalon tutorial.

First we will install virtualenv which will allow us to create virtual environments where we can install individual instances of python libraries. This allows us to have separate projects with their own library installations so that we can't wreck a project through modifying the python library installation for another.

To do this we can use our newly installed command line tool easy_install:

$ sudo easy_install virtualenv

We can set up virtual development environments by using

$ virtualenv DIRNAME/ENVNAME

which we can activate with

$ source DIRNAME/ENVNAME/bin/activate

where DIRNAME is the directory where the environment is installed and ENVNAME is the name of the environment.

After we activate an environment we can then procede to use pip, which comes with the virtualenv package to install several useful packages.

 

Step 3: homebrew

Now there are a few python packages that we need to install that have non-python dependencies. In order to install these we need to use a handy system called homebrew. To install homebrew we need to again bootstrap our system using the following line in bash:

$ /usr/bin/ruby -e "$(curl -fsSkL raw.github.com/mxcl/homebrew/go)"

which runs a ruby script to install homebrew.
Using homebrew we can install a few packages that we still need to get matplotlib and a few other nice tools up and running.

For example, numpy we will need us to install gfortran through homebrew

$ brew install gfortran

Step 4: pip install ...

Here we will install several useful packages, ipython, a useful python shell with several great features; npmath an arbitrary precision math library that Michele used in his compusalon example, numpy and scipy the numerical and scientific python library that we need for matplotlib.

$ pip install ipython 
$ pip install mpmath 
$ pip install numpy
$ pip install scipy


Step 5: matplotlib

The first of these tools we need is pkg-config which helps to configure matplotlib when it is being compiled.

$ brew install pkg-config

Next we need to install the libpng library as well as freetype as matplotlib will need to access these libraries in order to compile

$ brew install freetype

$ brew install libpng

$ brew install ffmpeg

We've added ffmpeg to allow us to save movies while using the excellent matplotlib.animation library. Now we are ready to install matplotlib

$ pip install matplotlib

Step 6: Ip[y]: notebooks

Ipython has come out with a very nice web-based notebook environment for running python scripts. It's very handy and allows you to run python like you would mathematica or matlab. You can follow the instructions in the link above, or the ones below to get started quickly.

First we need to add a few libraries/programs that the notebook environment needs:

$ brew install zeromq

$ pip install pyzmq

$ pip install Tornado

$ pip install jinja2

Finally for javascript equation rendering we can use the script provided by ipython itself to install MathJax:

(in iPython)

In [1]: from IPython.external.mathjax import install_mathjax

In [2]: install_mathjax()

Now everything should be set up to run your web-based notebook. To launch a local webserver at http://127.0.0.1:8888 (and also open up a default browser window to the page):

$ ipython notebook --pylab inline

here --pylab allows the use of the pylab graphics and inline puts the graphics in the notebook mathematica style, rather than opening separate windows. There are plenty of other details on how to use the web-based notebooks at the link above.

example image from ipython documentation

After installing iPy[notebook], I wanted a simple dock link that I could run to load iPy[notebook] server and start the web browser.

First, add the following paths to your .profile file so that bash knows where to look:

export PY_WORK_DIR=<your working directory where notebook files are saved>
export PY_ENV_DIR=<the python environment path>

Then open Automater, and create a new Application. Choose "Run AppleScript" and then enter

on run {input, parameters}

tell application "Terminal"
do script "cd $PY_WORK_DIR && source $PY_ENV_DIR/bin/activate && $PY_ENV_DIR/bin/ipython notebook --pylab inline"
end tell

return input
end run

Automator Script

And then save this wherever you like (I put mine in the Application folder) before dragging it onto the dock.

Alternatively you can just download and unpack this script.

Running this "application" should open up a terminal, start your python environment, activate a new iPy[notebook] web server, and start the web browser to the iPy[notebook] interface.

Step 7: Astronomy Specific Software

Here are a few packages that might be useful to astronomers (I'm not really sure since I'm a theorist):

APLpy is the astronomical plotting library (in python) package that is useful for doing stuff with FITS images. To install this we need a few packages, but it's all easily done with pip:

$ pip install pyfits

$ pip install pywcs

$ pip install APLpy

There is lots of info and tutorials on the APLpy page to check out!

ATpy and asciitable are python tools to read in tables of various formats useful for astronomy. They are also super easy to install in our setup:

$ pip install ATpy

$ pip install asciitable

Extra Links:

http://python4astronomers.github.com/plotting/matplotlib.html a nice matplotlib tutorial for scientific plotting