Function Details: __import__
Description
Imports a module. This is an advanced function that is not needed in everyday Python programming.
Extended Description
The __import__() function is a low-level function used by the import statement. It can be used to implement custom importing strategies. It's not recommended for general use; the import statement should be used instead. The function is useful in scenarios where you need to import modules dynamically based on runtime conditions. The 'globals' and 'locals' parameters are used to determine how to interpret the 'name' parameter in the context of packages. The 'fromlist' gives the names of objects or submodules that should be imported. The 'level' parameter specifies whether to use absolute or relative imports; 0 means absolute, while a positive number is the number of parent directories to search relative to the current module.
Read More about __import__ from Python Documentation
Function Signature
__import__(name: str, globals: Optional[dict] = None, locals: Optional[dict] = None, fromlist: List[str] = [], level: int = 0) -> module
Module: builtins
Parameters
Parameter List
- name: str
- globals: Optional[dict]
- locals: Optional[dict]
- fromlist: List[str]
- level: int
Return
Returns the imported module, or if 'fromlist' is given, the specified module attribute.
Return Type
module
Output
Explanation
This example shows how to use __import__() to import standard modules.