matplotlib + mac os x 10.7 (Lion)

Today I spent a good long chunk of my day shouting at my computer and trying to install matplotlib, a very handy python library. I finally solved it, so figured it was worth posting about here, in case anyone else has the same problem.

I recently updated my system python to version 2.7, so I’ve had to go through reinstalling various libraries that I use on a semi-regular basis. Matplotlib is one of these that up until now I’ve had no call for. However, my experiment project that I’m working on needs some pretty graphs, so I needed to get matplotlib installed.

I tried to install using pip, I tried to install using easy_install, and I tried to install by downloading the source and compiling it. None of these methods worked and I was often getting compiler errors like this:

llvm-g++-4.2: error trying to exec '/usr/bin/../llvm-gcc-4.2/bin/powerpc-apple-darwin11-llvm-g++-4.2': execvp: No such file or directory

Which is very annoying. So I spent some time googling, and it seemed that the reason for these errors is that the compiler was trying to build a universal version of some library – a version that would work on both 32 and 64 bit intel macs and older powerpc based macs. However, Apple have now removed all traces of powerpc support from osX and Xcode, so the compiler was failing. There is a way to add ppc support back to Lion, but either I was doing it wrong or those instructions are slightly out of date, because it didn’t solve my problem.  Now, depending on how I’d tried to fix the problem, it would fail with a lot of errors in compiling “ft2font.cpp”. Other times it would be accompanied with “i686-apple-darwin10-gcc-4.0.1: Internal error”. After uninstalling and reinstalling Xcode for the third time I realised I was going about this all wrong. I was trying to fix the problem, rather than look for why the problem was occuring. Why would my laptop try and build a universal library with powerpc support if it knew it couldn’t compile or run ppc software?

The answer lay with my version of python. Running

file $(which python)

revealed that my version of python was also universal:

/usr/local/bin/python: Mach-O universal binary with 3 architectures 
/usr/local/bin/python (for architecture i386): Mach-O executable i386 
/usr/local/bin/python (for architecture ppc): Mach-O executable ppc
/usr/local/bin/python (for architecture x86_64): Mach-O 64-bit executable x86_64

with support for ppc, i386 and x86_64 architectures. Because of this the compiler was trying to compile all the supporting libraries as universal as well.

I replaced python with the latest version (2.7.2), using the installer built only for i386/x86_64, and the problem was solved. Matplotlib built from source fine, and I’m pretty sure (though I didn’t test it) that I could have installed using pip or easy_install too.

A few hours spent swearing at the computer and an important lesson learned. Worth it.

One thought on “matplotlib + mac os x 10.7 (Lion)”

  1. Nice, I did already spend a few hours swearing at the computer (and various forums too for that matter), but your explanation has finally got me on the right track.

    I use pip to install all python packages, usually it works great and it’s just awesome, but occasionally some packages (cython, pyrex) fail to install with all kinds of strange complaints about gcc-4.2 (in snow leopard, xcode 4.2). At last I noticed that the problem is trying to compile some stuff using a powerpc compile which makes no sense at all as I’m on x86_64.

    My python 2.7 was indeed a universal build (via macports), after replacing it with non-universal the previously failing packages now install just fine. In case this might be useful to you someday, these were the steps to fix my env:
    # comment out +universal in /opt/local/etc/macports/variants.conf
    sudo port uninstall –follow-dependents python27
    sudo port install python27
    sudo port select –set python python27
    # clean up py27-pip and virtualenv27, virtualenv_select similarly

    Thanks a lot!

Leave a Reply