Given that OS X provides you with Python, you would expect it to be easy to install Python libraries on a modern Mac. Unfortunately, that’s not always true. Case in point: NumPy and matplotlib. These are my favorite tools nowadays for generic scientific computation and visualization. Going through the process of getting them to work for the nth time on a Mac, I decided to document the easiest way I found to do it, for my own future reference and in the hope that it helps someone.
Disclaimer: I’m doing this installation on Leopard (OS X 10.5) with Python 2.6.*. I assume the same procedure applies to later versions of OS X (yes, I’m a late adopter) and the Python 2 series.
So there are many ways to go about it, but this is the simplest one I found. It’s completely based on binary .dmg files:
Step 1: Download and install the latest “Python 2.6.* Mac OS X Installer Disk image” from the official python.org download pages.
This gives you an up-to-date python installation that doesn’t interfere with the system’s one.
To test, type
python --version
on the command line, and the version of Python you just installed should be reported.
Step 2: Download and install the latest Numerical Python .dmg binary for OS X and Python 2.6 from NumPy’s sourceforge download page.
To test, start a python interpreter and try this:
>>> import numpy
>>> numpy.__version__
Step 3: Download and install the latest matplotlib .dmg binary for OS X and Python 2.6 from matplotlib’s sourceforge download page.
To test, start a python interpreter and try this:
>>> import matplotlib
>>> matplotlib.__version__
Et voilá, this should do the trick.
Extra tip: If you use the shebang:
#!/usr/bin/env python
instead of:
#!/usr/bin/python
the version of python you want will be automatically used when you run your script from the command line.