1

I'm attempting to use lxml.etree. However, the import doesn't behave as I'd expect as can be seen below. What am I missing? I've never seen other modules behave like this.

$ python
Python 3.7.4 (default, Nov 18 2019, 17:30:21)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import lxml
>>> lxml.etree
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'lxml' has no attribute 'etree'
>>> from lxml import etree
>>> etree
<module 'lxml.etree' from '/home/me/env/project/lib/python3.7/site-packages/lxml/etree.cpython-37m-x86_64-linux-gnu.so'>
>>> lxml.etree
<module 'lxml.etree' from '/home/me/env/project/lib/python3.7/site-packages/lxml/etree.cpython-37m-x86_64-linux-gnu.so'>
>>>
zenzic
  • 1,517
  • 2
  • 16
  • 25
  • 1
    I believe it boils down to how lxml package was defined. If you dig into the site-packages you'll see there is an `__init__.py` that only contains a single function. So `import lxml` will only give you access to that one function. But because of the existence of the `__init__.py` you still have access to sub modules defined in lxml. Which is why `from lxml import etree` works – Orenshi Dec 10 '19 at 19:51
  • 1
    This is by design of authors. `lxml.etree` is likely a package and not a single module with functions. – Parfait Dec 10 '19 at 19:58

0 Answers0