Python Glossary Русский

all Python 3Built-in functionsterm

__import__

modules

Low-level module import. Normally use importlib.import_module instead.

Syntax

__import__(name, ...)

Application code almost never needs this — the function exists to back the import statement, and turning a module name held in a string into a module is importlib.import_module's job. There is also a dotted-name trap: with an empty fromlist you get the top-level package back, not the submodule, whereas import_module returns exactly what you asked for.

Examples

math = __import__('math')
print(math.pi)  # → 3.14159...
os = __import__('os')
print(os.sep)  # → /
json = __import__('json')
print(json.dumps({'a':1}))  # → {"a":1}
import importlib
re = importlib.import_module('re')
print(re.match(r'\d+','123').group())  # → 123

See also

Open in the full glossarydocs.python.orgReport an inaccuracy