CKTIS / Python
__import__()
Invokes the import mechanism using a module name supplied as a string.
Version: Python 3.14; repository image, production runtime not attested
Syntax
__import__(name, globals=None, locals=None, fromlist=(), level=0)Invokes the import mechanism using a module name supplied as a string.
With a dotted name an empty from-list returns the top-level package. A nonempty from-list returns the requested submodule.
The import mechanism loads the dotted module name, but its return depends on fromlist. Empty fromlist returns the top-level xml package, matching ordinary import binding. A nonempty fromlist returns xml.etree and can request attributes or submodules. Level zero selects an absolute import; a positive level is relative to the current package.
top = __import__("xml.etree")
leaf = __import__("xml.etree", fromlist=["ElementTree"])
assert top.__name__ == "xml" and leaf.__name__ == "xml.etree"
Examples
top = __import__("xml.etree")
leaf = __import__("xml.etree", fromlist=["ElementTree"])
print(top.__name__, leaf.__name__)
Result
xml xml.etree
The import mechanism loads the dotted module name, but its return depends on fromlist. Empty fromlist returns the top-level xml package, matching ordinary import binding. A nonempty fromlist returns xml.etree and can request attributes or submodules. Level zero selects an absolute import; a positive level is relative to the current package.