Monday, August 15, 2011

How to install python, ipython, numpy, scipy etc on Mac OS X Lion: The MacPorts way

Note added on March 7th 2012: This tutorial is deprecatedPlease refer instead to this updated tutorial.

I realized that by using MacPorts to install Python, as described in this tutorial, I am mixing libraries installed via MacPorts and the ones installed with easy_install which can lead to horrible incompatibility issues.

The updated tutorial describes how to get a working Scipy + Numpy + iPython + matplotlib installation using the built-in OS X Python and pip/easy_install.





Having trouble to get Scipy and IPython to work on Mac OS X Lion? Do you want the latest IPython? 

This post describes how to install Python (2.7), IPython (latest version is 0.12), Numpy, Scipy and other modules useful for astronomy on Lion. 

Tools required: Macports.
This will require around 15 min of your time to install all stuff. This estimate does not take into account the time required by macports to compile the required dependencies.

Instructions

1. Install MacPorts. Macports is a kind of apt-get for Mac. Then install python (v2.7), scipy, numpy, matplotlib and virtualenv using macports:

 sudo port install python27  
 sudo port install py27-numpy  
 sudo port install py27-matplotlib  
 sudo port install py27-scipy  
 sudo port install py27-virtualenv  

Macports will spend quite a while installing these packages and required dependencies. Expect it to take around one hour to do it since it will compile all the software on-the-fly for your architecture (in particular, gcc44 will take a long time to compile).

Make sure to select as macport's python as the default python in your mac instead of the one supplied by Apple:

 sudo port install python_select  
 python_select python27  

2. Create a directory somewhere which will hold the custom libraries you will install soon. In my example this directory will be ~/py:

 mkdir ~/py  

3. Use the virtualenv command to create a "virtual environment". virtualenv avoids mixing the libraries installed below with the system-wide ones installed with macports thus avoiding potential software conflicts:

 virtualenv-2.7 ~/py   

Activate the virtual environment:

 source ~/py/bin/activate  

Your terminal prompt should now display (py) before the input.

4. Install IPython, asciitables, cosmolopy, pyfits and any other modules and packages you may need using the program easy_install:

 easy_install ipython  
 easy_install asciitables  
 easy_install cosmolopy  
 easy_install pyfits   

It is important to run easy_install only after initiating the virtual environment as described in step 3. 

5. To facilitate the process of running ipython, add the following line to your ˜/.profile file: 

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

6. Now you should be able to run ipython by typing in the terminal:

 ipy  

Let me know if this works.




Note 1: I was having problems before with getting ipython 0.11, scipy and cosmolopy working using easy_install. That's when I decided to write this post. ipython was complaining about readline support. Scipy failed to install using easy_install (problem with ufmpack). Cosmolopy complained about importing scipy.special. Following the instructions above I managed to fix these problems.

Note 2: I heard that Enthought has a very nice Python distribution which is easy to install. Unfortunately, since I don't work at a degree-granting institution I don't have access to it.

Note 3: If you have no idea what asciitables, cosmolopy and pyfits are: they are modules very useful for astronomers. The first allows many ways of importing tables and ASCII data. The second is awesome for cosmological calculations. The third allows to import FITS files holding images and spectra.

2 comments:

  1. EPD Free is free for everyone, not just academics. It has a reduced package list, but still numpy/scipy/matplotlib. Academics get the full EPD package list for free.

    ReplyDelete
  2. The shortcoming of EPD Free in my case is that the Mac version does not support 64-bit

    ReplyDelete