Matplotlib with Eggs

I am really beginning to get excited about Python Eggs, from both a developer’s and installer’s perspective. I’ve been investigating packaging various parts of NDG as eggs and writing a fair amount of setuptools code recently.
Whilst I was burried in some of this code it occurred to me that eggs would allow me to experiment with various Python packages I never get arround to trying. A prime example is scientific packages. We are using Numeric and cdat in NDG but there’s several alternatives out there for data access , analysis and visualisation.

As a test I decided to see how quick and easy it would be to install these components using easy_install:

  1. numpy (note: NOT Numeric)
  2. matplotlib. For visualisation
  3. basemap. A toolkit package for matplotlib which adds map projection support.
  4. pycdf. A libnetcdf.a wrapper.

None of these projects distribute eggs and they all require C extension compilation so I wasn’t expecting it to work out of the box. Although I was right about that, easy_install got surprisingly far.

My test system was a linux-i686 Suse10.0 machine. Using the standard Python I checked distutils was installed, downloaded ez_setup.py and created a local setuptools installation directory on my PYTHONPATH.

$ easy_install numpy just worked as did $ easy_install matplotlib. The link from the Python cheeseshop to pycdf was broken so I had to download the tarball by hand. Once this was done $ easy_install -f . pycdf also worked thanks to libnetcdf.a being in a standard place. This was truely better than I had expected.

Unfortunately basemap wasn’t so helpful. Compilation went fine but I couldn’t import the module. Since this package contains data files I wasn’t surprised that it needed installing with $ easy_install --always-unzip and the BASEMAP_DATA_PATH environment variable needed setting but there was something else wrong.

Here my emersion in the setuptools system came to my rescue. The basemap package is designed to sit inside matplolib’s heirarchy. When two components are sitting in different eggs want to appear as a merged package tree they need to use a feature of setuptools called namespace_packages.

I dived into the basemap code and discovered it had been written with optional support for setuptools. However, getting namespace_packages working requires a very precise code layout and basemap had got it wrong. Once the problem was identified fixing it was straightforward:

$ pushd /matplotlib
$ cp toolkits/__init__.py .
$ popd

Now everything worked and I was able to create one of the standard basemap examples.

matplotlib/basemap example

It’s a pity basemap falls down on this small point and I hope it is fixed in the future. I know installing matplotlib can be a bit daunting and using easy_install definitely improved matters. It makes the prospect of deploying this software as part of a application server much more appealing.


About this entry