0

I want to get xmltodict in python2.7 up and running for a project I have, so I started by copy-pasting from the only example I was able to find

import xmltodict

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

print doc

Trying to run this results in the error: AttributeError: 'module' object has no attribute 'parse'

Same thing when trying to convert a dict to xml using the xmltodict.unparse function.

However, it works if I do this line by line in IDLE... Any idea why this fails when trying to run it in a .py file, but works when I use the interpreter line by line?

KBriggs
  • 1,220
  • 2
  • 18
  • 43

1 Answers1

3

Just don't name your script xmltodict.py. It is getting imported instead of an installed into the Python environment xmltodict package.

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • My script was called xml.py, not xmltodict.py. Renaming it 'test.py' did not change the error. – KBriggs Feb 05 '16 at 21:02
  • @KBriggs look for other scripts named `xmltodict` then. What is the value of `xmltodict.__file__` when you import it in your script? Thanks. – alecxe Feb 05 '16 at 21:06
  • 1
    I deleted xml.py and suddenly test.py worked. Apparently importing xmltodict was also importing xml.py? Maybe there is an import internal to xmltodict that imports xml.py and breaks everything. – KBriggs Feb 05 '16 at 21:32
  • 2
    So I guess the lesson here is "don't name your scripts anything vaguely related to the packages you are importing just on the off chance you duplicate something" – KBriggs Feb 05 '16 at 21:33