If I do this:
import lxml
in python, lxml.html
is not imported. For instance, I cannot call the lxml.html.parse()
function. Why is this so?
Importing a module or package in Python is a conceptually simple operation:
Find the .py file corresponding to the import. This involves the Python path and some other machinery, but will result in a specific .py file being found.
For every directory level in the import (import foo.bar.baz
has two levels), find the corresponding __init__.py
file, and execute it. Executing it simply means running all the top-level statements in the file.
Finally, the .py file itself (foo/bar/baz.py
in this case) is executed, meaning all the top-level statements are executed. All the globals created as a result of that execution are bundled into a module object, and that module object is the result of the import.
If none of those steps imported sub-packages, then those sub-packages aren't available. If they did import sub-packages, then they are available. Package authors can do as they wish.
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