2

I am making a script to perform creating and writing data to XML file. The error is no module no module name

I refer to this stackoverflow link, Python 2.5.4 - ImportError: No module named etree.ElementTree. I refer to this tutorial, https://stackabuse.com/reading-and-writing-xml-files-in-python/. I still do not understand on what is the solution. I tried to replace

"from elementtree import ElementTree"

to

"from xml.etree import ElementTree"

It still did not work.

#!/usr/bin/python
import xml.etree.ElementTree as xml
root = xml.Element("FOLDER")
child = xml.Element("File")
root.append(child)
fn = xml.SubElement(child, "PICTURE")
fn.text = "he32dh32rf43hd23"
md5 = xml.SubElement(child, "CONTENT")
md5.text = "he32dh32rf43hd23"
tree = xml.ElementTree(root)
with open(xml.xml, "w") as fh:
 tree.write(fh)

""" I expect the result to be that data is written to xml file. But I received an error shown below,

 File "./xml.py", line 2, in <module>
 import xml.etree.ElementTree as xml
 File "/root/Desktop/virustotal/testxml/xml.py", line 2, in <module>
 import xml.etree.ElementTree as xml
```ImportError: No module named etree.ElementTree
asked Jun 17, 2019 at 13:51
5
  • You are writing your own xml module and hiding the one in the stadard library. Commented Jun 17, 2019 at 14:03
  • Apologies. I did not copy the import xml module. However, after I added the import module it still did not work.Please kindly advise. Commented Jun 18, 2019 at 3:25
  • Do not call your file xml.py. Commented Jun 18, 2019 at 7:12
  • It is a name. What are the risk in calling the file xml.py Commented Jun 18, 2019 at 7:19
  • I already told you, you are hiding the module with the same name in the standard library. Commented Jun 18, 2019 at 7:22

2 Answers 2

2

import xml.etree.ElementTree as xml

and make sure you have the __init__.py file within the same folder if you use your own xml module and please avoid the path conflict.

then it will work.

answered Jun 17, 2019 at 15:41
Sign up to request clarification or add additional context in comments.

1 Comment

How is init.py related to this?
1

etree package is provided by "ElementTree" and "lxml" both are similar but it is reported that ElementTree have bugs in python 2.7 and works great in python3. I see you are using python 2.7 so lxml will work fine for you.

try this

from lxml import etree
from io import StringIO
tree = etree.parse(StringIO(xml_file))
# incase you need to read an XML.
print(tree.getroot())

And the StringIO is from default python io package. StringIO is neccessary when you are passing file to it (I mean putting XML in a file and passing that file to parser). It's good to keep it even tough you are passing XML as a big string. all the writing operations will be same for both.

answered Jun 17, 2019 at 14:45

8 Comments

While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.
Is the io module necessary? I thought lxml is sufficient.
@AbdullahNaina explanation give now.
I do not want to parse XML instead, I want to create and write XML file.
@AbdullahNaina, in that case you can use it without StringIO
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.