6

I'm on OSX Lion and I have libxml2 installed (by default) and I have python installed (by default) but they don't talk to one another. What's the simplest way to make this work on Lion?

$ python -c "import libxml2"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: No module named libxml2
John Mee
  • 50,179
  • 34
  • 152
  • 186

4 Answers4

14
  1. Visit ftp://xmlsoft.org/libxml2/python/ and ensure this is the latest version
  2. Download it
  3. unpack it
  4. open a term and cd to that directory
  5. type sudo python setup.py install
  6. ls /Library/Python/2.7/site-packages/ and you should see a bunch of libxml2 files
  7. try the test again and it should work

You'll need XCode installed, but like, you should already have that installed, right?

John Mee
  • 50,179
  • 34
  • 152
  • 186
tkone
  • 22,092
  • 5
  • 54
  • 78
  • I haven't found MacPorts to be horrible in any fashion, and it's a great source of other python modules, too. I can't speak for tkone's experience, but I highly recommend getting familiar with MacPorts if you find yourself working frequently with Python and other open source tools. – larsks Mar 21 '12 at 01:37
  • There's no reason to download and compile all of python just to get libxml2. At least homebrew does a much saner job of not screwing up anything you've already installed to a directory it wants to use. – tkone Mar 21 '12 at 02:59
  • ...whereas I have found homebrew to have a much smaller package selection and a less useful package management system. As I said, I'm personally happy with MacPorts, your mileage may vary, try out the alternatives and use what meets your needs. Cheers! – larsks Mar 21 '12 at 03:11
  • I'm not advocating homebrew either. I'd just rather use virtualenv, pip and the occasional `python setup.py install`, which, thanks to virtualenv is local only to my virtualenv. no messing with anything on the system please. – tkone Mar 21 '12 at 03:12
  • For package in pypi, you don't even need to manually download, unzip and run `python setup.py install`. Just `pip install` in virtualenv and you're done. – Kien Truong Mar 21 '12 at 03:35
  • I had tried pip but just pip install libxml didn't work and I was too lazy to search for it :) – tkone Mar 21 '12 at 11:02
  • 1
    failed to find headers for libxml2: update includes_dir – Rodrigo Vieira Mar 29 '19 at 17:44
7

tkone's answer is OK. But brew also can do it.

Try this,

  $ brew install libxml2 --with-python
jeswang
  • 1,017
  • 14
  • 26
0

I don't believe the stock Python ships with the libxml2 module. You can install macports, and then do this:

$ sudo port install py27-libxml2

...and after a short wait while everything installs, end up with Python 2.7, libxml2, and the libxml2 Python module.

larsks
  • 277,717
  • 41
  • 399
  • 399
-4

Just use Pypi to install python module packages, by typing :

sudo pip search libxml  and sudo pip install libxml
Veelion Chong
  • 23
  • 1
  • 5
  • I don't think you tested this before you posted. Doesn't work. The package is lxml, but even that doesn't install very cleanly in my experience. – Gattster Dec 12 '13 at 01:22