我在 Windows 上运行 Python 2.5.4,并且在尝试导入 ElementTree 或 cElementTree 模块时不断收到错误消息。代码非常简单(我正在学习教程):
import xml.etree.ElementTree as xml
root = xml.Element('root')
child = xml.Element('child')
root.append(child)
child.attrib['name'] = "Charlie"
file = open("test.xml", 'w')
xml.ElementTree(root).write(file)
file.close()
当我从 cmd 运行它时会收到错误消息,或者当我直接从 Python 解释器尝试它时不会收到错误消息。
Traceback (most recent call last):
File "C:\xml.py", line 31, in <module>
import xml.etree.ElementTree as xml
File "C:\xml.py", line 31, in <module>
import xml.etree.ElementTree as xml
ImportError: No module named etree.ElementTree
另外,我检查了模块在 C:\Python25\Lib\xml\etree 中