Sunday, 4 September 2011

Python 2.7, numpy and scipy for OS X

Get the 2.7 python installer for OS X from here (You will need the developer tools from Apple installed!).

For easy installing of python packages, get easy_install for python 2.7. Get the correct .egg, and execute it as a shell command:

sh easy_install_something.egg


I had some trouble installing scipy using easy_install, so instead of using easy_install, a better tool is to use pip. It can be installed using

sudo easy_install-2.7 pip

if easy_install was already installed, otherwise instructions are on the website. I'm not sure if pip needs to be linked to a specific python version (I have 4 versions of python installed at the moment on my Macbook :( ), but it worked fine the first time.

Python packages can be installed using:

sudo pip install numpy
sudo pip install scipy

in that order.

pip freeze

shows the currently installed packages and

sudo pip uninstall x

uninstalls package 'x'.

This will install the most recent stable versions of numpy and scipy (numpy 1.5.1 and scipy 0.80 at the time of writing). The reason I suggest pip over easy_install is because easy_install failed to install scipy for me and pip was written to replace easy_install as it has some nice additional features like listing the currently installed packages and the ability to uninstall packages.

I also suggest installing 'nose':

sudo pip nose

With nose, you can test if your numpy/scipy installation is correctly installed by opening a python terminal and typing:

import numpy
numpy.test('1','10')
import scipy
scipy.test('1','10')

Another useful command you can use to check out the currently installed packages and the related documentation is to type:

pydoc2.7 -g

which opens a small python window, which will open a web browser with the requested information.

Other interesting packages as suggested on the numpy homepage:
ipython
matplotlib

IMPORTANT:
There is a bug in matplotlib, where it isn't compatible with the newest version of numpy when loading the pylab module (matplotlib ImportError: No module named ma). If it gives this error, you can fix it by editing the following files:

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/numerix/ma/__init__.py
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/numerix/npyma/__init__.py

and replacing

from numpy.core.ma import *

with

from numpy.ma import *

No comments:

Post a Comment