Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

lxml error on Windows - AttributeError: module 'lxml' has no attribute 'etree'

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

like image 762
user2497748 Avatar asked Dec 09 '16 18:12

user2497748


1 Answers

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".

like image 136
Ya Zawsze Avatar answered Oct 11 '22 23:10

Ya Zawsze