Python Programming, news on the Voidspace Python Projects and all things techie.

matplotlib and numpy for Python 2.7 on Mac OS X Lion

emoticon:pencil Unfortunately, due to an API change, the latest released version of matplotlib is incompatible with libpng 1.5. Take a wild guess as to which version comes with Mac OS X Lion. :-/

Fortunately this is fixed in the matplotlib repository. Here's how I got matplotlib working on Mac OS X Lion (with Python 2.7 - but these instructions should work fine for other versions of Python too).

First matplotlib requires numpy. The latest version is 1.6.1, from here. The precompiled Mac OS X binaries are compiled to be compatible with Mac OS X 1.3 and up, which means they are 32 bit only. By default Python will run as 64 bit on OS X Lion, which means you'll see this when attempting to import numpy:

>>> import numpy
Traceback (most recent call last):
ImportError: dlopen(/.../site-packages/numpy/core/, 2): no suitable image found.  Did find:
        /.../site-packages/numpy/core/ no matching architecture in universal wrapper

You can get round this by launching python as a 32 bit process. I have the following alias in my .bash_profile:

alias py32="arch -i386 python"

The next problem is the matplotlib one. This blog entry shows how to build matplotlib from the git repo, using homebrew. I don't want to use a homebrew installed Python, so I modified the instructions to only install the dependencies with homebrew. I also set the correct flags to compile a 32bit version of matplotlib to match the 32bit numpy.

brew install pkg-config
brew install gfortran
cd matplotlib
export ARCHFLAGS="-arch i386"
py32 build
py32 install

And it appears to work. So far anyway:

>>> import pylab
>>> x = pylab.randn(10000)
>>> pylab.hist(x, 100)

Like this post? Digg it or it.

Posted by Fuzzyman on 2011-09-05 00:18:13 | |

Categories: , , , Tags: , ,

Hosted by Webfaction