Why does importing a python module not import nested modules?

我与影子孤独终老i 提交于 2020-06-21 03:57:45

问题


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?


回答1:


Importing a module or package in Python is a conceptually simple operation:

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

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

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




回答2:


lxml is called a package in Python, which is a hierachical collections of modules. Packages can be huge, so they are allowed to be selective about what is pulled in when they are imported. Otherwise everybody would have to import the full hierarchy, which would be quite a waste of resources.




回答3:


It's by design. The package has the option to import the nested package in its __init__.py, then, you would be able to access the nested package without problems. It's a matter of choice for the package writer, and the intent is to minimize the amount of code that you probably won't use.




回答4:


lxml is a package, not a module. A package is a collection of modules. As it happens, you can also import the package directly, but that doesn't automatically import all of its submodules.

As to why this is, well, that's a question for the BDFL. I think it's probably because packages are generally quite large, and importing all the submodules would be an excessive performance penalty.




回答5:


This is to allow only the minimum amount of code to have to be loaded for multi-part libraries that you may not use the entirety of. For instance, you might not be using the html part of lxml, and thus not want to have to deal with loading its code.



来源:https://stackoverflow.com/questions/9303179/why-does-importing-a-python-module-not-import-nested-modules

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!