Python Error : ImportError: No module named 'xml.etree'

42,786

Solution 1

Remove the file xml.py or a directory xml with a file __init__.py in it from your current directory and try again. Python will search the current directory first when importing modules. A file named xml.py or a package named xml in the current directory shadows the standard library package with the same name.

As pointed out in a comment by KeshV, you also need to remove the file xml.pyc, if it exists. In Python 2 it will be in the same directory as xml.py. In Python 3 it will be in the sub directory __pycache__. In General, as long as the *.py file is around, you can savely delete the corresponding *.pyc file because Python will re-create it upon import of the *.py file.

Solution 2

Your script name is: xml.py?

Change it and it should work.

Solution 3

A local .py file overwrites the pre-defined module. Remove all the files that named xml in the current folder.

Share:
42,786

Related videos on Youtube

AbtPst
Author by

AbtPst

Interested in learning about Machine Learning and NLP.

Updated on August 18, 2020

Comments

  • AbtPst
    AbtPst almost 4 years

    I am simply trying to parse an XML file:

    import xml.etree.ElementTree as ET
    tree = ET.parse('country_data.xml')
    root = tree.getroot()
    

    but this gives me:

    import xml.etree.ElementTree as ET
    ImportError: No module named 'xml.etree'
    

    I am using Python 3.5. I have tried to same code with Python 2.7 and 3.4 but I always get this error. I thought that the XML libraries come as standard. Also, I can see that in my Lib folder:

    enter image description here

    So why can't it pick up the module? I am really confused. Do I have to make some change in an environment variable somewhere?

    Please help.

    • AbtPst
      AbtPst over 8 years
      never mind, false alarm. my dev package was named xml. i changed the name and now it works
  • AbtPst
    AbtPst over 8 years
    i dont have a file of that name
  • AbtPst
    AbtPst over 8 years
    my file is called parse.py
  • Mike Müller
    Mike Müller over 8 years
    Do you have a sub-directory xml in there?
  • AbtPst
    AbtPst over 8 years
    yes, my package was named xml. now i changed the name and it works :) thanks
  • KeshV
    KeshV almost 8 years
    also remove xml.pyc , just removing xml.py is not enough
  • Mike Müller
    Mike Müller almost 8 years
    @KeshV Thanks for the comment. Added a section to my answer mentioning this.
  • Richard
    Richard about 7 years
    Thank you! Now I understand a little more about how python works.
  • kuga
    kuga over 3 years
    is there an alternative, if i want to keep my name?