xmltodict 在 .py 文件中使用时抛出错误

xmltodict throwing errors when used in a .py file

我想在 python2.7 中获取 xmltodict 并 运行ning 我有一个项目,所以我从我唯一的例子开始复制粘贴能够找到

import xmltodict

with open ('test.xml') as fd:
    doc = xmltodict.parse(fd.read())

print doc

尝试 运行 这会导致错误:AttributeError: 'module' object has no attribute 'parse'

尝试使用 xmltodict.unparse 函数将字典转换为 xml 时也是如此。

但是,如果我在 IDLE 中逐行执行此操作,它会起作用...知道为什么在 .py 文件中尝试 运行 它时会失败,但当我使用解释器行时会起作用线?

只是不要将脚本命名为 xmltodict.py。它是 getting imported 而不是安装到 Python 环境 xmltodict 包中。