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

百般思念 提交于 2019-12-04 00:29:10

问题


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.


回答1:


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'>



回答2:


I had the same problem as you.

My problem was that I named one of my Python files lxml.py.

You should always check if your Python file's name conflicts with a module.



来源:https://stackoverflow.com/questions/13749916/receiving-importerror-cannot-import-name-etree-when-using-lxml-in-python-on-m

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!