简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM