__import__() Low-level import

Last updated: 2026-09-22

__import__() Low-level import

__import__ is the low-level function behind the import statement; prefer the import statement unless you need dynamic imports.

CategoryBuilt-in Functions
KindBuilt-in Function
Python Versionall

📝 Syntax

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

⚙️ Parameters

name RequiredThe module name (string).
globals OptionalThe global namespace dict.(Default:None)
locals OptionalThe local namespace dict.(Default:None)
fromlist OptionalNames of submodules or attributes to import.(Default:())
level OptionalRelative import level; 0 means absolute.(Default:0)

Returns:The top-level module object (not the attributes in fromlist).

💥 Raises

▶ Example

Edit the code, press Run, and see the result in the output panel:

Output:

4.0
⚠️ Warning Dynamic loading of untrusted module names is a security risk; use `import` for ordinary cases.
More case studies coming soon — check back later.

❓ FAQ

QWhy not call __import__() directly instead of using the import statement?
AThe import statement handles name binding, fromlist semantics, and other details; __import__() is just a low-level implementation function — use import for everyday importing.
QWhat does __import__('math') return?
AIt returns the math module object. Note that it returns the top-level module: for import a.b, __import__ returns a rather than b.
QWhat should I use for dynamic imports?
APrefer importlib.import_module(name); it returns the specified module rather than the top-level one, giving clearer semantics than __import__.
QIs dynamically importing an untrusted module name risky?
AYes — it may load and execute arbitrary code; when the module name comes from user input, strictly validate it against a whitelist.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏