1

First: I know that anyone who wants to help will ask for code that demonstrates the error. That will require a ZIP of the project, and I don't see how to attach a file to a StackOverflow question. I'll be happy to upload the file when someone tells me how.

This is one of those things where "I didn't change anything, but it broke." The environment is Windows 10, Python 3.8, and PyCharm 2019.3.5.

I left the project in a fully debugged state a couple of weeks ago. Today I added a function definition and a call to it. Now the program fails when it tries to create a parser for an XML tree... before the new function is ever called.

Early in the script I import etree from xml:

from xml import etree

At the point of failure I try to create a parser:

_parser = etree.ElementTree.XMLParser(encoding="iso-8859-1")

The messages I get are:

Connected to pydev debugger (build 193.7288.30)
Traceback (most recent call last):
  File "C:/Users/... /PartConfig/PartConfig.py", line 47, in <module>
    _parser = etree.ElementTree.XMLParser(encoding="iso-8859-1")
AttributeError: module 'xml.etree' has no attribute 'ElementTree'

I have an "except" block, but it never gets executed because its scope is etree.ElementTree.ParseError.

Taken at face value, the error message is simply wrong. I know the script found etree.ElementTree because it ran past the import statement, and when I misspelled the module name as an experiment it failed right there. ElementTree is an element of xml.etree in the standard Python library, so I can't think of a way the script could fail the way it did. The message must be trying to tell me something, but what?

Jonathan Sachs
  • 613
  • 8
  • 19

2 Answers2

2

You have to use this syntax:

from xml.etree import ElementTree
_parser = ElementTree.XMLParser(encoding="iso-8859-1")

As @Fred Larson explained in his comment, you have to import the module itself, and etree is a package.

arsalan
  • 83
  • 6
  • 3
    This is because `xml.etree` is a [*package*, not a *module*](https://stackoverflow.com/q/7948494/10077). `ElementTree` is a module within the `xml.etree` package. – Fred Larson Sep 29 '21 at 18:55
0

I know the script found etree.ElementTree because it ran past the import statement

No. The import statement is from xml import etree. This mentions nothing about ElementTree.

ElementTree is an element of xml.etree in the standard Python library

etree is a directory. That directory contains a file named ElementTree.py, but you don't get access to it simply by importing etree. (You would if etree/__init__.py imported ElementTree, but in this case it doesn't).

As @arsalan said, you have to explicitly import ElementTree.

John Gordon
  • 29,573
  • 7
  • 33
  • 58
  • I'm sorry if I did not get my terminology exactly right. The import statement I used is exactly as arsalan et. al. specified. As I said, this code was completely debugged, i.e. it is written correctly and was working. /// I think I need to attach the project to the question for us to get further, but as I said, I don't see a means of doing that on the page. – Jonathan Sachs Sep 30 '21 at 00:45
  • I got this to work. I had to import the etree package _and_ the ElementTree module: `from xml import etree` and `from xml.etree import ElementTree`. /// I reread the `import` doc, and I understand why I need to do this. I don't understand why I didn't need it last week (when I could have found the error more easily because it wouldn't have been in a part of the script that I hadn't changed). Can anyone propose a theory? /// It could be explained by a change in xml.etree's __init__.py, but there has been none; the file is timestamped from when I installed Python 3.8 in 2019. – Jonathan Sachs Oct 01 '21 at 13:14
  • One explanation would be that the offending line simply didn't execute in your earlier testing. Does that line only execute under certain conditions? – John Gordon Oct 01 '21 at 14:59
  • The offending line reads the XML input file. The script wouldn't do anything useful without it. – Jonathan Sachs Oct 03 '21 at 10:42