PyXML on Leopard

January 17th, 2008

I attempted to install PyXML on Mac OS X 10.5 today and ran into a problem: I was unable to load any of the PyXML modules:


>>> from xml import xpath
Traceback (most recent call last):
  File "“, line 1, in 
ImportError: cannot import name xpath`

So, the problem originates from this nasty hack that allows PyXML to contribute modules to Python’s standard xml namespace.

On Leopard, simply installing PyXML will not make the PyXML modules magically appear in the xml namespace as on other platforms. If you take a look at:

/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/xml/__init__.py

you’ll see why:


# only prefer _xmlplus if the environment variable PY_USE_XMLPLUS is defined
if os.environ.has_key('PY_USE_XMLPLUS'):`

So, the solution is to set that environment variable and your PyXML packages will all magically appear. I think this is new to 10.5 because I didn’t find any links from Google about it (just the Darwin source code for __init__.py).

Comments are closed.