我正在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()
Run Code Online (Sandbox Code Playgroud)
当我从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
Run Code Online (Sandbox Code Playgroud)
另外,我检查了C:\ Python25\Lib\xml\etree中的模块