• ImportError: No module named dom.minidom

    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 so 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 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 to 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.

    Saturday, November 1st, 2008 at 05:21
  • Sunday, December 7th, 2008 at 12:28 | #1

    sir, you have helped me greatly.
    my file was also named xml.py, i owe you some beers.

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
TOP