How can I import a module dynamically given its name as string?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
If you need to import a Python module whose name is only known at runtime, the standard solution is importlib.import_module(). That approach is clearer and safer than trying to build source strings and execute them, and it works naturally with normal Python import rules.
The standard API: importlib.import_module
This is the direct modern answer. It behaves like a normal import, except the module name is provided dynamically.
It also supports dotted module paths.
Why not use __import__ directly
Python still has the lower-level built-in __import__, but importlib.import_module() is easier to read and usually the better choice in application code.
So unless you are doing something very specialized, use importlib.
Loading a class or function by name
Often the real goal is not “import a module” but “load a specific object from a module.” In that case, import first and then resolve the attribute.
This is common in plugin systems, task runners, and configuration-driven frameworks.
Handling errors cleanly
Dynamic imports fail for predictable reasons, so catch them intentionally.
If you are loading an attribute as well, you may also need to catch AttributeError.
Importing from a file path
If the code is not installed as an importable package, you can still load it from a file using importlib.util.
This is useful for plugin folders, internal tooling, and experiments, but it is more advanced than ordinary imports and should be used deliberately rather than as a default loading mechanism.
This is useful for plugin directories or internal tooling, but it is a more advanced path than ordinary module import by name.
A common plugin pattern
This pattern keeps configuration data separate from hardcoded imports.
Security and design caution
Dynamic imports are powerful, but do not treat arbitrary untrusted strings as import targets without validation. If a user controls the module name completely, they may be able to load modules you never intended to expose.
In many systems the safer design is a registry of approved plugin names mapped to import targets, so configuration stays flexible without turning import resolution into a free-form execution surface.
A safer design is often to allow only a known set of approved module names or entry points.
Common Pitfalls
A common mistake is using exec() or dynamically constructed import statements instead of importlib.
Those approaches are harder to read, harder to validate, and usually less safe than using the import APIs Python already provides.
Another mistake is forgetting that a dynamic import can raise both module-level and attribute-level errors.
A third mistake is letting unvalidated external input drive import targets directly in a plugin or configuration system.
Summary
- Use
importlib.import_module()for ordinary dynamic imports by string name. - Use
getattr()after import if you need a specific function or class. - Use
importlib.utilwhen importing from a file path rather than an installed module name. - Handle
ModuleNotFoundErrorandAttributeErrorcleanly. - Validate dynamic import targets when the names come from external input.
- Prefer a small approved registry over unrestricted import strings in production systems.

