OS X: Installing Python and its scientific computing packages

OS X: Installing Python & its scientific computing packages

While the overall recommendations here will not change, the details will always be dated. These notes were written 28 March 2006 and updated 2 April 2008. If you're reading this much after these dates, you may encounter changes or, in fact, may benefit from more up-to-date procedures and packages.

Basic OS X Environment:

Software Package Management: Fink

Installing third-party and open-source software under OS X is greatly simplified by Fink, a port of the Debian Linux apt and dpkg systems for package management. Download and install Fink. Then download and install FinkCommander—a very convenient GUI interface to fink. (Make sure it's at least version 0.5.4.) Learn how to use it.

Most of what you will need is available as Fink packages. Within the FinkCommander GUI, packages can be selected and installed. Fink tracks dependencies and will prompt you to install other packages when necessary.

An alternative installation path for Python and the main scientific packages (SciPy and NumPy), that does not use fink but requires more direct hands-on steps, is described here.

Fink packages

Some package installations take quite a while. This is especially true when installing from source, since compiling is involved. The first time setting up fink, in particular, requires patience. Once the main packages are installed, then further upgrades and additions will take markedly less time.

Some of the packages listed below (e.g., scipy-py25 and matplotlib-py25) are classified by fink as "unstable". This does not make them unusable, but rather indicates that they have been tested less than the stable packages. Configure fink to "use unstable packages" so that these can be located: within the FinkCommander GUI, find the appropriate checkbox under Preferences -> Fink.

If you're already using fink, now would be a good time to update the packages you currently have installed. In particular, update fink itself: Source -> Selfupdate. I usually then run Source -> Scanpackages and then Source -> Utilities -> Index. Now look for the outdated ones and install them to bring them up to date. This will avoid the new packages below relying on potentially, slightly out-of-date existing packages.

With fink installed and running, the basic installation process is to use it (or Fink Commander) to install the following packages: python25, ipython-py25, scipy-py25, scipy-core-py25, visual-py25, scientificpython-py25, matplotlib-py25, gnuplot-py25, pil-py25, pygame-py25, and ImageMagick.

Here are some comments on the packages themselves, installation issues, links, and documentation. I give installation tests for some of the packages. It's a good idea to get each package running, testing as suggested, before moving along. Occasionally, the tests discuss Unix commands and shells. To understand these please refer to any basic Unix/Linux introduction. (Several tutorials are listed here.)

Python

Python: Even though OS X 10.5 (Leopard) has Python version 2.5 pre-installed (in /usr/bin/python), you should install the fink's python25 and related packages. The main reason is that visual Python does not have an installation for Leopard's Python 2.5. (If this situation changes, then I'll update these notes for using the latter.) The fink version puts a new and up-to-date python in /sw/bin/python and associated libraries in /sw/lib/python25. All other Python-related packages will be installed in /sw/lib/python25/site-packages.

Let's test the installation: In a Terminal window, type

which python
You should see
/sw/bin/python
If not, then try `rehash' and the `which' command again. If this doesn't solve it, then you need to modify your shell environment path to have /sw/bin and it should appear before other directories.

Run python:

python
You should see something like:
Python 2.5.2 (r252:60911, Mar 23 2008, 14:25:24) 
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 
The important thing is that it says version 2.5 is running. Hit Ctrl-D to exit.

Developing Python

iPython is a very convenient shell in which to develop and run Python. Install the package ipython-py25. The manual is here.

Let's test it:

ipython
The first time you may be asked to approve final set-up for iPython. Approve the final set up. (I also ran the %upgrade it suggested; quit ipython by typing Ctrl-D, and restarted it.) Next time you run iPython you should see something like
Python 2.5.2 (r252:60911, Mar 23 2008, 14:25:24) 
Type "copyright", "credits" or "license" for more information.

IPython 0.8.2 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object'. ?object also works, ?? prints more.

In [1]: 
Hit Ctrl-D to exit:
Do you really want to exit ([y]/n)? y

Python Scientific Computing

scipy-py25 and scipy-core-py25

SciPy is a Python module that contains algorithms for integrating ODEs, optimizing functions, and so on.

The scipy.org web site contains information on building SciPy under Mac OS X; see the alternative installation note and link above. The fink install outlined here is generally more reliable and manages all the various dependencies that otherwise one has to do oneself.

In particular, installing scipy-py25 will also load in (scipy-core-py25) (aka NumPy). This is a numerical computation package which is a port of the standard (and optimized) numerical linear algebra routines from LAPACK, BLAS, and the like.

SciPy and NumPy tutorials and documentation are here.

scientificpython-py25

Scientific Python is set of higher-level computational methods than SciPy.

Unfortunately, it does not use NumPy, but it requires an older Python numerical package (numeric). Since we're installing using fink, fink will take care of this dependency and will download and install the additional package.

Python Visualization

visual-py25

VPython or Visual Python is a 3D graphics environment. The visual package that we'll use offers real-time 3D output and is easily usable by novice programmers. It also uses the older numeric package and, again, fink will take care of installing it.

Test installation: Start up the X11 window server. (It's down in Applications -> Utilities -> X11.app.) Then move over to the examples subdirectory:

cd /sw/lib/python2.5/site-packages/visual/examples/
Run an example:
python lorenz.py
Exit the demo by hitting the red Quit button in the window's upper lefthand corner.

NOTE: If the shell prompt does not return, start another Terminal window, do 'ps -ax', find the python command and its process ID number PID, and then 'kill PID'.

Run another:

python gas.py

matplotlib-py25

Matplotlib is a MATLAB-inspired mostly-2D plotting package for Python. Tutorials and manuals are available from that home page.

Copy the matplotlib configuration file over to your home directory:

cp /sw/lib/python2.5/site-packages/matplotlib/mpl-data/matplotlibrc ~/.matplotlib/

Test installation: First, again make sure the X11 window server is running (see above). Then go check out the examples:

cd /sw/share/doc/matplotlib-py25/examples
Run an example:
python bar_stacked.py
Let's see if iPython and matplotlib are talking:
ipython -pylab 
Should get you a prompt:
In[0]:
Type:
In[0]: plot([1,2,4,8])
This should produce a simple plot.

pygame-py25

PyGame provides a fast, though primitive, interface for 2D graphics.

gnuplot-py25

Gnuplot is a 2D and semi-3D plotting package that has been around the Unix world for some time.

pil-py25

Python Imaging Library (PIL) adds image processing to Python and supports a range of image file formats, along with graphics capabilities.

ImageMagick

ImageMagick provides a complementary set of utilities for manipulating images.

Currently, we use its display utility to draw image files on the screen. The ImageMagick display utility and other pieces of ImageMagick require XWindows. Installing Apple's X11 opens a world of functionality and so is strongly recommended for any computational science user on the Mac.

There is a workaround for displaying images not through X11, however. The OS X Preview application will display image files. So instead of calling "display filename" to use ImageMagick's display, call "open /Applications/Preview.app filename", even if this is being done in a system call within python.

Thanks:

These notes are adapted from OS X install notes by Chris Myers.