The Offbytwo Blog

software, security and startups

Installing lxml on OS X Leopard

Update: the post below is about installing lxml 2.0 (2.0.5 is the specific version I installed).

One way to install lxml is to install the py25-lxml package from MacPorts, but I hate installing python packages from MacPorts as it insists on installing it’s own version of Python. I like installing setuptools and using easy_install to manage my Python packages. I got lxml to install just fine in the past, but when I tried to do it again last night I ran into some problems so I figured this time I’m going to write down exactly how I solved it so I can refer back to it next time.

I vaguly remembered from last time that I needed to install libxml2 and libxslt in order to build lxml. So I used MacPorts to install libxml2 and libxslt and I tried building lxml again. Still had the same problems as before. It seems that Leopard comes with a version of libxml2 (/usr/include/libxml2), but lxml refuses to build against it (seems to be missing some header files, I’m guessing it’s the wrong version but I didn’t investigate further). It seems that lxml was picking up the system libxml2 instead of the MacPorts version.

Looking through the lxml documentation I found some options for specifying the library to build against

python setup.py build --with-xslt-config=/path/to/xslt-config

If this doesn’t help, you may have to add the location of the header files to the include path like:

python setup.py build_ext -i  -I /usr/include/libxml2

I don’t really like this options since I really want to do easy_install lxml and have it work. But the first suggestion got me thinking that maybe I need to change my path to have /opt/local/bin before /usr/bin. So I added /opt/local/bin in front of my PATH and sure enough lxml now installs with easy_install without a problem.

Update: Kumar discovered a possible problem with my instruction. See his comments below for the details. Until version 2.0.6 is released set CFLAGS=’-flat_namespace’ before easy_install lxml

A summary for the lazy readers:

sudo port install libxml2 libxslt
export PATH=/opt/local/bin:$PATH
export CFLAGS=’-flat_namespace’
sudo easy_install lxml

May 7, 2008 - Posted by cosmin | Tutorials | , , , | 5 Comments

5 Comments »

  1. Cosmin, you rock. This got me up and running after I found a way to fix the broken libxml2 port. doh!
    http://trac.macports.org/ticket/15230

    Comment by Kumar McMillan | May 9, 2008

  2. ack, spoke too soon. I’m still getting segfaults but … setting this during runtime is fixing the problem:

    export DYLD_LIBRARY_PATH=/opt/local/lib

    of course that breaks svn and anything else that expects the default dylibs. *sigh*. Maybe I’ll try a static build

    Comment by Kumar McMillan | May 10, 2008

  3. grumble. I still can’t find a good fix for this; maybe one will turn up on http://codespeak.net/pipermail/lxml-dev/2008-May/003609.html

    However, I did find a better solution vs. above. It is to set

    export DYLD_FORCE_FLAT_NAMESPACE=1

    at runtime. This doesn’t conflict with subversion and solves the problem in that “/usr/lib/libxml2.2.dylib uses two-level namespace, meaning that the
    Foundation framework will always use this one instead of yours” — from http://0xced.blogspot.com/2006/07/dealing-with-outdated-open-source-libs.html

    I’d still like to find a way to accomplish this at compile time instead.

    Comment by Kumar McMillan | May 11, 2008

  4. ok, just for safe keeping, I’m finishing off this thread. The bug has been fixed and a patch is going into 2.0.6. In the meantime, this should work:

    sudo port install libxml2 libxslt

    export PATH=/opt/local/bin:$PATH

    export CFLAGS=’-flat_namespace’

    sudo easy_install http://codespeak.net/lxml/lxml-2.0.5.tgz

    Details:
    http://codespeak.net/pipermail/lxml-dev/2008-May/thread.html#3632

    Comment by Kumar McMillan | May 16, 2008

  5. Thanks, I’ve updated the instructions in the post to use -flat-namespace. As a precaution I’m going to rebuild lxml on my machine the same way.

    Comment by cosmin | May 16, 2008

Leave a comment