How to use libxml2 with python on macOs?

12,797

Solution 1

  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?

Solution 2

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

Try this,

  $ brew install libxml2 --with-python
Share:
12,797
John Mee
Author by

John Mee

I am mee, and no-one else.

Updated on June 14, 2022

Comments

  • John Mee
    John Mee almost 2 years

    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
    
  • larsks
    larsks about 12 years
    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.
  • tkone
    tkone about 12 years
    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.
  • larsks
    larsks about 12 years
    ...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!
  • tkone
    tkone about 12 years
    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.
  • Kien Truong
    Kien Truong about 12 years
    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.
  • tkone
    tkone about 12 years
    I had tried pip but just pip install libxml didn't work and I was too lazy to search for it :)
  • Gattster
    Gattster over 10 years
    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.
  • Rodrigo Vieira
    Rodrigo Vieira about 5 years
    failed to find headers for libxml2: update includes_dir
  • Rodrigo Vieira
    Rodrigo Vieira about 5 years
    Error: invalid option: --with-python