__Import__ in Python
It’s known that Python’s import statement is implemented by __import__ function. In general, if we want to import a module dynamically, we can use import_module function, which is a wrapper around __import__.
The most important difference between these two functions is that import_module() returns the specified package or module (e.g. pkg.mod), while import() returns the top-level package or module (e.g. pkg). – https://docs.python.org/3/library/importlib.html#importlib.import_module
import itertools and from requests import exceptions can be translated to:
| |
__import__
This is an advanced function that is not needed in everyday Python programming, unlike importlib.import_module().
Here is an example of how __import__ is called:
| |
As we mentioned earlier, the __import__ returns the top level module.
For example, requests=__import('requests.exceptions',globals(),locals(),[],0). If you want to get the submodule exceptions, you need to use getattr: equests_exceptions=getattr(__import__('requests', globals(), locals(), [], 0), 'exceptions').
There is another tricky way to import the submodule: use a non-empty fromlist: requests_exceptions = __import__('requests.exceptions', globals(), locals(), [None], 0).
Additionally, we can also set fromlist to specify the names of submodules that should be imported. The statement from spam.ham import eggs, sausage as saus can be translated to
| |
Skip importing non-existing modules with __import__
This a use case of the __import__ function. Some packages are missing, but we want to make sure that the code does not crash when importing them.
| |