Wednesday, August 15, 2012

How to install a scientific Python environment on Mac OS X Mountain Lion

I updated my Macbook Pro to Mountain Lion (hereafter ML) and I wanted to see if my previous Python installation tutorial is valid also for ML. It turns out that most of the commands still work, but some of them need a little fine-tuning.

Please use this guide if you want to get a working scientific Python environment (Python + Scipy + iPython + Numpy + matplotlib) under Mac OS X Mountain Lion. The installation will be based on the Python binary that comes by default with OS X (i.e. Xcode).

Here are the steps:
  1. Install the requirements: Xcode which includes Python (via App Store), gfortran (via Macports), virtualenv, additional libraries required by matplotlib
  2. Create a python environment with virtualenv
  3. Install Numpy, Scipy, matplotlib, ipython with pip. Install readline with easy_install
  4. Create an alias in .profile or .bash_profile (depending on your shell) to run ipython
After these steps are completed, you will get a working Python environment for scientific analysis, visualization and statistics with Mac OS X Mountain Lion. 

1. How to get the requirements working

Xcode + Python

Launch the App Store and download Xcode. After it is installed, open it and go to Xcode's preferences. There, go to the Downloads tab, look for Command Line Tools and click Install. That should install the  default OS X Python binary (these instructions were inspired by this post).

gfortran

In my case, I installed gfortran by installing MacPorts and installing GCC which comes with gfortran:

 sudo port install gcc44  

To make gfortran visible to the system I created an alias in /usr/local/bin:

 cd /usr/local/bin/  
 sudo ln -s /opt/local/bin/gfortran-mp-4.4 gfortran  

virtualenv

I went to web page that hosts virtualenv and downloaded virtualenv.py. You will use virtualenv.py below.

Additional libraries required by matplotlib (optional)

I use the graphical backend TkAgg, which requires the following additional libraries for matplotlib to work: tk, freetype, libpng. I installed them using macports:

sudo port install tk
sudo port install freetype
sudo port install libpng

2. Create a python environment with virtualenv

Create a directory stdpy (in my example) somewhere and issue the command

 /usr/bin/python virtualenv.py stdpy  

to create an isolated python environment based on the python provided by default with Mac OS X. This avoids trouble with mixing libraries. Activate the environment by running

 source stdpy/bin/activate  

You should now see a (stdpy) showing up in your terminal.

3. Install Numpy, Scipy, matplotlib, ipython with pip and readline with easy_install

After activating the python environment, let's proceed and install the additional modules with pip and easy_install:

pip install numpy
pip install git+https://github.com/scipy/scipy#egg=scipy-dev
pip install git+https://github.com/matplotlib/matplotlib.git#egg=matplotlib-dev
easy_install readline
pip install ipython  

The reason why issuing simply the commands "pip install scipy" and "pip install matplotlib" do not work is explained in this blog post.

You may need to install additional libraries in order to get matplotlib compiled, depending on the kind of graphical backend that you choose. In my case, I use TkAgg which depends on Tk, freetype and libpng libraries which I installed via macports.

4. Create an alias in .profile or .bash_profile (depending on your shell) to run python

In my case I use Bash and I added the following line to the file .bash_profile in my home directory:

 alias ipy='source ~/stdpy/bin/activate && ipython --pylab'   

Now, when I open the terminal and issue the command

 ipy  

it will automatically activate the python environment and run ipython.


Let me know if these instructions work well for you.

Changelog:

  • Aug. 18 2012: added matplotlib compilation requirements
  • Sep. 1st 2012: made explanation about matplotlib dependencies clearer (hopefully)

5 comments:

  1. It is now time I believe to make such a great guide for python 3.

    This guide helped me a lot with my existing install of Python 2 and I am very thankful for it! Great job man!

    Could you also adapt it to work for version 3? I suppose a few extra steps need to be made in order to make python3 the default python language and stuff like that.

    Thank you for an excelent work.

    ReplyDelete
  2. An error occured for me while installing gfortran. After googling for solution, turned out that it is a common problem. The fix is re-agreeing to the license agreement, i.e., typing "sudo xcodebuild -license".

    ReplyDelete
  3. Hi there, great blog.

    I just have one problem at the scipy stage. When I try to pip install git+https://github.com/scipy/scipy#egg=scipy-dev, it just hangs with:

    Downloading/unpacking scipy-dev from git+https://github.com/scipy/scipy#egg=scipy-dev
    Cloning https://github.com/scipy/scipy to ./stdpy/build/scipy-dev

    Is there an alternative route? I'm using mountain lion

    Thanks

    ReplyDelete
    Replies
    1. Have you checked if there are any firewall issues where you are trying to download scipy-dev? I am not aware of an alternative route for this.

      Delete
  4. Using gcc44 instead of something more recent is what I was missing (on OS X 10.7). Thanks

    ReplyDelete