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
5
  • You are writing your own xml module and hiding the one in the stadard library. 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.
    – user10068582
    Jun 18, 2019 at 3:25
  • Do not call your file xml.py. Jun 18, 2019 at 7:12
  • It is a name. What are the risk in calling the file xml.py
    – user10068582
    Jun 18, 2019 at 7:19
  • I already told you, you are hiding the module with the same name in the standard library. 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.

1
  • How is init.py related to this?
    – user10068582
    Jun 18, 2019 at 3:43
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.

8
  • 2
    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.
    – Nic3500
    Jun 17, 2019 at 22:35
  • 1
    Is the io module necessary? I thought lxml is sufficient.
    – user10068582
    Jun 18, 2019 at 3:27
  • @AbdullahNaina explanation give now.
    – Kiran Gali
    Jun 18, 2019 at 6:03
  • I do not want to parse XML instead, I want to create and write XML file.
    – user10068582
    Jun 18, 2019 at 6:17
  • @AbdullahNaina, in that case you can use it without StringIO
    – Kiran Gali
    Jun 18, 2019 at 6:31

Your Answer

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