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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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.bazhas two levels), find the corresponding__init__.pyfile, and execute it. Executing it simply means running all the top-level statements in the file.Finally, the .py file itself (
foo/bar/baz.pyin 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.