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
| Name | Type | Description |
|---|---|---|
| name | str | Module or package name. |
| globals | dict | None | Calling module context for relative imports. |
| locals | mapping | None | Accepted for compatibility; standard implementation does not use it. |
| fromlist | iterable of str | Names requested from the module; influences return module. |
| level | int | 0 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)