ImportError: No module named dom.minidom

2008 Oct 31 at 23:51 » Tagged as :weird, xml, python,

I Thought, I would try my hand at XML parsing with the python DOM. Whatever programming llanguage you use the DOM pretty much the the same. However things didn't get off to an auspicious start. Even the sample code on the python docs didn't work and produced an error:

Traceback (most recent call last): File "/var/www/clients/work/python/xml.py", line 3, in <module> from xml.dom.minidom import parse, parseString File "/var/www/clients/work/python/xml.py", line 3, in <module> from xml.dom.minidom import parse, parseString ImportError: No module named dom.minidom

Then I saw a suggestion saying try from xml.dom.minidom import * . I didn't see how that will work but gave it a try anyway with no luck. I next checked to see if I have libxml2-python I thought to update it and python to see if it will solve it - it didn't. Next I tried the interpreter (just invoking python from the command line) and cutting and pasting the code there - whoa that works! Next I tried `cat xml.py | python` and lo and behold that works too. Then I noticed that a file named xml.pyc is being created by the interpreter and being left behind. I suspected that the filename or the way the interpreter is being executed might have something to do with. A post by one Matt Mower confirmed my fears: The python script being named as xml.py causes the intepreter to get confused. It's conflicting with the python module named xml (in java we call these packages) in the library. Renaming the file as my_domxml.py and deleting the xml.pyc did the trick. Note that if you just rename the file it's not going to work the .pyc file will continue to cause trouble. So I guess whenever I see an ImportError: No module named xxx I should immidiately check my choice of filename.