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
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".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With