Python 2.5.4 - ImportError: No module named etree.ElementTree
Asked Answered
B

4

9

I'm running Python 2.5.4 on Windows and I keep getting an error when trying to import the ElementTree or cElementTree modules. The code is very simple (I'm following a tutorial):

import xml.etree.ElementTree as xml

root = xml.Element('root')
child = xml.Element('child')
root.append(child)
child.attrib['name'] = "Charlie"
file = open("test.xml", 'w')
xml.ElementTree(root).write(file)
file.close()

I get the error message when I run it from the cmd or but not when I directly try it from the Python interpreter.

Traceback (most recent call last):  
File "C:\xml.py", line 31, in <module>
  import xml.etree.ElementTree as xml   
File "C:\xml.py", line 31, in <module>
  import xml.etree.ElementTree as xml
ImportError: No module named etree.ElementTree

Also, I checked and the module is there in C:\Python25\Lib\xml\etree

Blearyeyed answered 18/6, 2010 at 20:57 Comment(4)
You don't get the error message in your subject from running the code that you posted. You would get that message from import etree.ElementTree. When asking for help, copy/paste both your code and the traceback & error message, don't type from memory.Flitting
You have changed your displayed code but it still doesn't produce the error message in the question's subject. In fact it appears to work. Apart from using "file" as a variable name, it looks OK. Do you still have a problem? If so, what? If not, you should have just edited your question so that it reflected what the original problem was.Flitting
If the interpreter works, and cmd doesn't, is it possible that you have an old version of python running from cmd?Temperamental
I checked that, but I only have one version of Python installed.Blearyeyed
N
7

You missed the very important line in the tutorial

import xml.etree.ElementTree as xml

This makes xml.etree.ElementTree now known as xml throughout the module.

I happen to have python 2.5.4 and I have verified that the same code you have above works:

user@Comp test$ cat test.py 
import xml.etree.ElementTree as xml

root = xml.Element('root')
child = xml.Element('child')
root.append(child)
child.attrib['name'] = "Charlie"
file = open("test.xml", 'w')
xml.ElementTree(root).write(file)
file.close()

user@Comp test$ /usr/bin/python2.5 --version
Python 2.5.4
user@Comp test$ /usr/bin/python2.5 test.py 
user@Comp test$ cat test.xml 
<root><child name="Charlie" /></root>user@Comp test$ 

So check and make sure you're running python 2.5.4 and if you are try reinstalling. The problem is not that it's python 2.5.4 or your code. It's some installation problem, you're running a different version of python, or there's some other strange problem.

Noyade answered 18/6, 2010 at 21:26 Comment(2)
@Jen: Post the actual code that you ran, the traceback, and the error message. "doesn't recognise the module" is ambiguous.Flitting
Thanks! I ended up downloading effbot.org ElementTree package and installing it. It is working now, but I still have no idea why the original library wasn't working...Blearyeyed
B
48

Because your original file name is C:\xml.py

Change the file name to any other name

Banzai answered 25/4, 2012 at 22:25 Comment(2)
Oh god the pain. Also don't forget to remove the "xml.pyc" file, as well!Inhesion
I just ran into the exact same problem. It just seemed logical to name my script xml.py since I was going to use it to try out the xml libs for the first time to learn them. Didn't think about the script's name screwing with the Python namespace. :) Thanks for noticing this.Forget
T
12

I got the same error report("ImportError: No module named etree.ElementTree") when naming the test file as xml.py. And it got fixed when I renamed it to something else like xmltest.py.

Tomasz answered 1/9, 2012 at 2:45 Comment(0)
N
7

You missed the very important line in the tutorial

import xml.etree.ElementTree as xml

This makes xml.etree.ElementTree now known as xml throughout the module.

I happen to have python 2.5.4 and I have verified that the same code you have above works:

user@Comp test$ cat test.py 
import xml.etree.ElementTree as xml

root = xml.Element('root')
child = xml.Element('child')
root.append(child)
child.attrib['name'] = "Charlie"
file = open("test.xml", 'w')
xml.ElementTree(root).write(file)
file.close()

user@Comp test$ /usr/bin/python2.5 --version
Python 2.5.4
user@Comp test$ /usr/bin/python2.5 test.py 
user@Comp test$ cat test.xml 
<root><child name="Charlie" /></root>user@Comp test$ 

So check and make sure you're running python 2.5.4 and if you are try reinstalling. The problem is not that it's python 2.5.4 or your code. It's some installation problem, you're running a different version of python, or there's some other strange problem.

Noyade answered 18/6, 2010 at 21:26 Comment(2)
@Jen: Post the actual code that you ran, the traceback, and the error message. "doesn't recognise the module" is ambiguous.Flitting
Thanks! I ended up downloading effbot.org ElementTree package and installing it. It is working now, but I still have no idea why the original library wasn't working...Blearyeyed
F
5

I had a funny situation, which may or may not be similarly to this and found my solution. I created my own module for parsing xml files. I put it in my_project_root/utilities/xml.py. When import xml.etree.ElementTree or xml.etree from within this module I would get the error in the title of this posting. It was searching in itself, so from within xml.py it was attempting to import etree.ElementTree, and could not find a package or module named etree. I changed the name of my module to xml_parse.py and deleted my_project_root/utilities/xml.pyc and it worked perfectly. A simple reminder of using caution with module naming conventions.

Foochow answered 17/1, 2012 at 22:12 Comment(1)
Yes, you are right, I replace the name, and remove the .pyc file and it work. It's very funny this issue.Task

© 2022 - 2024 — McMap. All rights reserved.