etree 导入不工作- Python 2.7.5

etree import not working- Python 2.7.5

我正在尝试使用 elementtree 来解析 xml 但导入时出现错误。

没有名为 etree 的模块

我试过了:

from xml.etree import ElementTree as ET

import xml.etree.ElementTree as ET

我在 mac 上的 python 库中下载了 elementtree。我错过了什么吗?

谢谢

ElementTree is a part of the standard Python library,您无需安装任何东西即可运行。

使用 standard install from IdeOne, shows your first line works, with no extra libraries.

from xml.etree import ElementTree as ET
print ET

给出:

Success time: 0.03 memory: 44896 signal:0

<module 'xml.etree.ElementTree' from '/usr/lib/pypy/lib-python/2.7/xml/etree/ElementTree.py'>

您的安装出现问题,或者您正在尝试使用 third party library like lXML


或者,you have a file named xml.py 在您的项目中覆盖您的 Python 安装。您可以在任何其他导入之前运行文件中的以下代码检查这一点。

import xml
print xml.__file__

如果路径看起来不是这样,并且显示了您的本地项目的路径,那就是您的问题。

/usr/lib/python2.7/xml/__init__.pyc

删除任何名为 xml 的本地文件并删除它们的编译版本。 Python 最初在本地搜索 xml,因此可能会发生冲突。