16

I am using Anaconda v4.2 with Python 3.5 on Windows 32 bit, and wanting to use lxml etree. My Anaconda distribution includes lxml 3.6.4, but the only lxml function that my IDE (PyCharm, although I'm getting the same error when running the code with Jupyter Notebook) can see is get_include(). The following code:

import lxml
full_xml_tree = lxml.etree.parse('myfile.xml')

just gives me the error:

AttributeError: module 'lxml' has no attribute 'etree'

I also tried installing the VisualC++ compiler for Windows, but that hasn't made any difference. I tried reinstalling lxml using conda on the command line, again no change to my error. What am I missing? It seems like the lxml.get_include() function isn't finding any of the files to include, and I don't really understand how the etree.cp35-win32.pyd file (which I assume contains the compiled etree code??) should be being associated with the lxml package. Any help much appreciated!

Cathy

user2497748
  • 171
  • 1
  • 1
  • 3
  • 1
    Have you saved the script you're working on (or some other script in the working directory) as `lxml.py`? In that case Python is importing that file instead of the desired module. – kindall Dec 09 '16 at 18:16
  • Thanks for the response kindall :-) No, I haven't but I do have another version of lxml that I'm using for other projects using Python 2.7. But it's in a separate virtual environment, so that shouldn't affect anything, should it? – user2497748 Dec 12 '16 at 09:05
  • Funny enough, when I do from lxml import etree the problem does not occur. – Konstantin Jun 29 '17 at 10:40

1 Answers1

21

This is a bit of a quirk in how the etree (ElementTree) subpackage is imported.

You have to import the subpackage explicitly for it to be available:

import lxml.etree
full_xml_tree = lxml.etree.parse('myfile.xml')

The recommended way to achieve what you're trying to do would be to import the ElementTree module:

import xml.etree.ElementTree as ET
tree = ET.parse('myfile.xml')

See: https://docs.python.org/3.6/library/xml.etree.elementtree.html

Why does this happen?

Imagine a package with a directory structure like this:

test_pkg/__init__.py
test_pkg/shown_module.py
test_pkg/hidden_module.py

and where the __init__.py contains the following:

from . import shown_module

using this package you use shown_module directly:

>>> import test_pkg
>>> test_pkg.shown_module
<module 'test_pkg.shown_module' from '.../test_pkg/shown_module.py'>

But hidden_module can't be used directly:

>>> test_pkg.hidden_module
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'test_pkg' has no attribute 'hidden_module'

But it can be used if imported:

>>> import test_pkg.hidden_module
>>> test_pkg.hidden_module
<module 'test_pkg.hidden_module' from '.../test_pkg/hidden_module.py'>

However, I do not know why ElementTree is "hidden".

qff
  • 5,524
  • 3
  • 37
  • 62
Ya Zawsze
  • 337
  • 2
  • 5
  • Thanks for providing code which might help solve the problem, but generally, answers are much more helpful if they include an explanation of what the code is intended to do, and why that solves the problem. – Neuron May 25 '18 at 13:01
  • If I change my code due to your recommendation i can not evaluate xpath-expression anymore due to `AttributeError: 'ElementTree' object has no attribute 'xpath'` – anion Jun 07 '21 at 21:18