LSD::RELOAD

August 31, 2008

how to install lxml python module on mac os 10.5 (leopard)

Filed under: Python — Leo Simons @ 11:38

lxml is an xml library for python that doesn’t suck. It needs a recent libxml2 and libxslt. Mac OS X does not come with recent versions, and 10.5 breaks completely if you force it to try and use a recent version.

I used to use MacPorts for everything (which comes with py25-lxml) but ran into some issues with 10.5. So back to manual installs, it is.

  • assuming your python is the mac os x default…
  • make sure no traces of other pythons in your $PATH
  • download and install libxml2:
       ./configure --prefix=/usr/local/libxml2-2.7.0
       make
       sudo make install
       cd /Library/Python/2.5/site-packages
       sudo ln -s /usr/local/libxml2-2.7.0/lib/python2.5/site-packages/* .
    
  • download and install libxslt:
       ./configure --prefix=/usr/local/libxslt-1.1.24 --with-libxml-prefix=/usr/local/libxml2-2.7.0
       make
       sudo make install
       cd /Library/Python/2.5/site-packages
       sudo ln -s /usr/local/libxslt-1.1.24/lib/python2.5/site-packages/* .
    
  • download and install lxml:
       sudo python setup.py install \
         --with-xml2-config=/usr/local/libxml2-2.7.0/bin/xml2-config \
         --with-xslt-config=/usr/local/libxslt-1.1.24/bin/xslt-config
    

Update March 1, 2009: in order to get libxml2 and libxslt python bindings working on 10.5.6, with a python.org 2.6 version of python, I had to do quite a bit more fiddling. If the above doesn’t work for you, try this:

cd libxml2-2.7.0
autoreconf
./configure --prefix=/usr/local/libxml2-2.7.0
make
sudo make install
cd ../libxslt-1.1.24
autoreconf
./configure --prefix=/usr/local/libxslt-1.1.24 \
    --with-python \
    --with-libxml-prefix=/usr/local/libxml2-2.7.0
make
sudo make install
cd ../libxml2-2.7.0/python

# libxml2 build doesn't support do -arch ppc
export ARCHFLAGS='-arch i386'

# setup.py supports libxslt install, sort-of
cp ../../libxslt-1.1.24/python/libxsl* .
cp ../../libxslt-1.1.24/python/generator.py xsltgenerator.py
cp ../../libxslt-1.1.24/doc/libxslt-api.xml .

# hack the setup.py file to learn the dir structure
mv setup.py setup.py.bak
curl 'http://pastebin.com/pastebin.php?dl=f158d0a96' > setup.py
python setup.py build
sudo python setup.py install

And then install lxml normally.

(the use of autoreconf is explained by DarwinPorts folks, the use of ARCHFLAGS is explained by apple, and the hand-editing/hand-merging of libxslt and libxml python bindings for the build based on experimental fiddling.)

15 Comments »

  1. thanks for posting this! made my day!

    cheers,

    tom

    Comment by Tom Lazar — September 24, 2008 @ 17:24 | Reply

  2. Yep, very useful. It’s easy to install on Linux, but on Mac installs can be a pain… I think I would have given up lxml without your explanation, and possibly Python altogether. Thanks a lot.

    Comment by Farialima — October 21, 2008 @ 22:33 | Reply

  3. Thank you so very-very much! I was soaping a rope… Not anymore! :) ))

    Comment by lorochka — November 30, 2008 @ 23:49 | Reply

  4. [...] how to install lxml python module on mac os 10.5 (leopard) « LSD::RELOAD (tags: python xml osx) [...]

    Pingback by links for 2008-12-15 « Amy G. Dala — December 15, 2008 @ 15:01 | Reply

  5. Thanks :)
    Saved my day.

    Comment by amerikatt — December 22, 2008 @ 23:29 | Reply

  6. I struggled with installing libxml2 and the related python packages for Python 2.4. I tried building, compiling, scratching and clawing but had trouble with my specific needs. I’m no expert but wrote up what worked for me here:

    http://taocode.blogspot.com/

    I needed this package for IMS Transport and Marshal XML all for Plone. Thank you for providing this information, hopefully my experience can help others get up and running too.

    Comment by Mark Jones — January 15, 2009 @ 22:39 | Reply

  7. Thanks a lot. I did not exactly follow your steps, but I made it with fink and the sources. I had multiple python installation, but gave the correct path in compilation. It worked.

    Comment by sesh — February 3, 2009 @ 12:31 | Reply

  8. Thanks, you made my day!

    Comment by Peter — February 10, 2009 @ 22:57 | Reply

  9. Awesome — been trying to figure this one out — very helpful

    Comment by Luke — March 10, 2009 @ 16:43 | Reply

  10. YOU RULE. Thank you so much for this!

    Comment by Dorothea Salo — March 13, 2009 @ 15:40 | Reply

  11. Thanks! I followed your steps exactly, except that i used libxml2-2.7.3 instead of libxml2-2.7.0. I had tried to get lxml installed a few times since January and failed each time. (I was missing the –prefix and –with–prefix options, as well as the site-packages symlinks) Can’t understand why they don’t just make the documentation on the lxml/libxml2/libxslt sites more comprehensive. Thanks again!

    Comment by metametagirl — March 13, 2009 @ 23:16 | Reply

  12. Apparently I’m missing something. I’ve been through these instructions a couple of times now and I keep coming up against the same error every time I try install lxml. Everyone else commenting on this post seems to have gotten it to work for them, anyone have the secret?

    I’m running 10.5.6 with the default python 2.5 installation. the only thing I’ve done that differed from the post is use libxml2-2.7.3.

    The error:

    Building against libxml2/libxslt in one of the following directories:
    /usr/local/libxml2-2.7.3/lib
    /usr/local/libxslt-1.1.24/lib
    ld warning: in /usr/local/libxslt-1.1.24/lib/libxslt.dylib, file is not of required architecture
    ld warning: in /usr/local/libxslt-1.1.24/lib/libexslt.dylib, file is not of required architecture
    ld warning: in /usr/local/libxml2-2.7.3/lib/libxml2.dylib, file is not of required architecture
    ld warning: in /usr/local/libxslt-1.1.24/lib/libxslt.dylib, file is not of required architecture
    ld warning: in /usr/local/libxslt-1.1.24/lib/libexslt.dylib, file is not of required architecture
    ld warning: in /usr/local/libxml2-2.7.3/lib/libxml2.dylib, file is not of required architecture
    No eggs found in /tmp/easy_install-EUw2w0/lxml-2.1.5/egg-dist-tmp-0HIXao (setup script problem?)
    error: Could not find required distribution lxml==2.1.5

    Thanks in advance for any help!

    Comment by TJ Ward — March 17, 2009 @ 14:41 | Reply

  13. Hi TJ Ward,

    I ran into the same problems and same error message as yours. That might be obvious to the other readers but it wasn’t for me. You need the setuptools package installed (providing the easy_install utility). You can download and find the installation instructions here:

    http://pypi.python.org/pypi/setuptools

    It solved everything for me. Hope that’s your problem and that fixes it.

    Thanks to the blog author for this easy walkthrough!

    Regards,
    Jimmy

    Comment by Jimmy Royer — March 24, 2009 @ 7:09 | Reply

  14. I have just followed this tutorial, using the instructions from “Update March 1, 2009″. But instead of hacking setup.py I’ve used the original file and executed “sudo python2.6 setup.py install –with-xml2-config=/usr/local/lib/libxml2-2.7.3/bin/xml2-config –with-xslt-config=/usr/local/lib/libxslt-1.1.24/bin/xslt-config”. Note that the libs are grouped under the /usr/local/lib directory.

    Comment by Henry — March 27, 2009 @ 14:51 | Reply

  15. [...] (Assuming libxml is installed like this) [...]

    Pingback by Install CouchDB on Mac OS X (without using DarwinPorts) « LSD::RELOAD — March 29, 2009 @ 16:51 | Reply


RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.