ID EN
Built-in Functions

__import__

Python 3.11

This function is invoked by the import statement. It can be replaced (by importing the builtins module and assigning to builtins.__import__) in order to change semantics of the import statement, but doing so is strongly discouraged as it is usually simpler to use import hooks (see PEP 302) to attain the same goals and does not cause issues with code which assumes the default import implementation is in use. Direct use of __import__() is also discouraged in favor of importlib.import_module().

Syntax

PYTHON
__import__(name, globals=None, locals=None, fromlist=(), level=0)

Examples

Example 1
PYTHON
spam = __import__('spam', globals(), locals(), [], 0)
Example 2
PYTHON
spam = __import__('spam.ham', globals(), locals(), [], 0)
Example 3
PYTHON
_temp = __import__('spam.ham', globals(), locals(), ['eggs', 'sausage'], 0)
eggs = _temp.eggs
saus = _temp.sausage

See Also

importlib.import_module() import builtins __import__() importlib.import_module() import __import__() __import__()