Built-in Functions and Types

__import__()

Invoke the low-level import operation used by import statements. A dotted name with an empty fromlist normally returns the top-level package; a nonempty fromlist returns the named module. Relative imports use level and module context. A direct call does not perform the import statement’s local name binding. Prefer import statements for ordinary code or importlib.import_module for programmatic module lookup. Importing runs module initialization code, so import only trusted modules.

Formal Syntax & Structure

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

Parameters & Attributes

NameTypeDescription
namestrModule or package name.
globalsdict | NoneCalling module context for relative imports.
localsmapping | NoneAccepted for compatibility; standard implementation does not use it.
fromlistiterable of strNames requested from the module; influences return module.
levelint0 for absolute imports, positive for relative imports.

Return Value

module object

Top-level package or requested module, depending on name and fromlist.

Working Example

Example Snippetpython
# Trusted standard-library modules only.package = __import__("collections.abc")module = __import__("collections.abc", fromlist=("Iterable",))print(package.__name__)print(module.__name__)print(module.Iterable is not None)
Python Reference