【发布时间】:2010-06-18 20:57:29
【问题描述】:
我在 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 中
【问题讨论】:
-
运行您发布的代码时,您不会在主题中收到错误消息。您会从
import etree.ElementTree收到该消息。寻求帮助时,复制/粘贴您的代码和回溯和错误消息,不要从内存中输入。 -
您已经更改了显示的代码,但它仍然不会在问题的主题中产生错误消息。事实上,它似乎工作。除了使用“文件”作为变量名之外,它看起来还可以。你还有问题吗?如果是这样,是什么?如果没有,您应该刚刚编辑您的问题,以便它反映原始问题是什么。
-
如果解释器工作,而 cmd 没有,是否有可能从 cmd 运行旧版本的 python?
-
我检查过了,但我只安装了一个版本的 Python。
标签: python import elementtree