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

Nov 1st, 2008 | Posted in Uncategorized
Tags: , ,
  1. Dec 7th, 2008 at 12:28 | #1

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

  2. Sean
    Apr 10th, 2009 at 19:44 | #2

    dude, you rock I owe you a good bottle scotch

  3. abhi
    Jul 10th, 2009 at 13:16 | #3

    you are my hero man

  4. DerMac
    Jul 23rd, 2009 at 00:01 | #4

    Hi there,

    maybe this comment makes this thread easier to find:

    “ImportError: No module named dom”

    when trying to execute:

    “python xml.py”

    -> rename your xml file to s.th. different than “xml.py” then it will work

    no need to install pyXML!!!

    Thank you for this post!!!!

    • admin
      Apr 28th, 2010 at 16:50 | #5

      DerMac, thanks for summarizing

  5. Vectar
    Nov 10th, 2009 at 20:37 | #6

    no kidding, great find, saved me a ton of time. Thanks!

  6. mop
    Feb 3rd, 2010 at 00:56 | #7

    wow,after spending nearly one hour to solve this problem, google suggest this blog… thanks a lot

  7. jm
    Aug 4th, 2010 at 09:46 | #8

    Really very very smart! thank you a lot

  8. Li
    Aug 9th, 2010 at 11:26 | #9

    thanks ,it’s very useful

  9. coinsil
    Jan 12th, 2011 at 21:09 | #10

    god bless you!

  10. Loic
    Feb 16th, 2011 at 20:44 | #11

    Thank you verry much, you save me a lot of time.

  11. Apr 1st, 2011 at 13:05 | #12

    Same problem here, thanks for blogging about it!

  12. Rakesh
    May 25th, 2011 at 12:34 | #13

    Thanks a lot mate saved me a lot of time

  13. remi
    Aug 9th, 2012 at 23:06 | #14

    Thank you!

  14. Feb 20th, 2013 at 09:08 | #15

    Thanks it is very helpful …

  15. May 28th, 2013 at 00:01 | #16

    i’m grateful!

    thanks!

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>