Receiving 'ImportError: cannot import name etree' when using lxml in Python on Mac

Craig Alleva picture Craig Alleva · Dec 6, 2012 · Viewed 14.4k times · Source

I'm having difficulty properly installing lxml for Python on Mac. I have followed the instructions here, which after installation indicates that the installation is successful (however, there are some warnings. The full log of the install and warnings can be found here)

After running the install, I am trying to run Test.py in the lxml install directory to ensure that it's working correctly. I am immediately prompted with the error:

ImportError"  cannot import name etree.  

This error results from the line from lxml import etree.

I can't seem to figure out why it's failing here after a seemingly successful install. Please forgive my ignorance, as I don't typically program in Python and certainly never on a MAC (forced to do so at the moment).

For reference:

  • Python Version: 2.7.2
  • Mac OS X 10.8.2

Thanks in advance for all the help.

Answer

Yuval Adam picture Yuval Adam · Dec 6, 2012

Sounds like you have another lxml in your path. Make sure you are referencing the right one, it should look something like this:

>>> import lxml
>>> lxml
<module 'lxml' from '/path/to/lib/python2.7/site-packages/lxml/__init__.pyc'>