importlib
The importlib package provides the implementation of the import statement in pure Python, and exposes the components of the import system for importing modules programmatically or building custom importers. That access also supports dynamic imports and manipulation of the import process at runtime.
Here’s a quick example:
>>> import importlib
>>> math_module = importlib.import_module("math")
>>> math_module.sqrt(16)
4.0
Key Features
- Imports modules programmatically
- Reloads modules to reflect code changes without restarting the interpreter
- Accesses and manipulates the underlying import system
- Customizes module import behavior
- Reads distribution metadata and package data files through
importlib.metadataandimportlib.resources
Frequently Used Classes and Functions
| Object | Type | Description |
|---|---|---|
importlib.import_module() |
Function | Imports a module programmatically using its name |
importlib.reload() |
Function | Reloads a previously imported module |
importlib.util.find_spec() |
Function | Finds the module specification for a given module name |
importlib.metadata.version() |
Function | Returns the installed version of a distribution package |
importlib.resources.files() |
Function | Returns a traversable object for a package’s non-code resources |
Examples
Programmatically importing a module:
>>> import importlib
>>> random_module = importlib.import_module("random")
>>> random_module.randint(1, 10)
Reloading a custom module after making changes:
>>> import custom_module
>>> ## Make changes to custom_module...
>>> importlib.reload(custom_module)
Finding a module’s specification:
>>> import importlib.util
>>> importlib.util.find_spec("collections")
ModuleSpec(name='collections', ...)
Common Use Cases
- Dynamically importing modules at runtime
- Reloading modules to reflect changes without restarting the Python interpreter
- Customizing or extending the import mechanism for special use cases
- Accessing module specifications for introspection purposes
Real-World Example
Suppose you want to dynamically import and use a module based on user input. You can do this using importlib:
>>> import importlib
>>> module_name = input("Enter the module to import: ")
Enter the module to import: math
>>> module = importlib.import_module(module_name)
>>> module.sqrt(16)
4.0
In this example, the importlib package allows for dynamic importation of a module specified by the user, enabling flexible and customizable module usage within the application.
Related Resources
Tutorial
Python Zip Imports: Distribute Modules and Packages Quickly
In this step-by-step tutorial, you'll learn what Zip imports are and how to use them in Python. You'll learn to create your own importable ZIP files and make them available for use. Finally, you'll learn how to use the zipimport module to dynamically import code from ZIP files.
For additional information on related topics, take a look at the following resources:
- Python's __all__: Packages, Modules, and Wildcard Imports (Tutorial)
- Python import: Advanced Techniques and Tips (Tutorial)
- Absolute vs Relative Imports in Python (Tutorial)
- Absolute vs Relative Imports in Python (Course)
- Advanced Python import Techniques (Course)
- Managing Imports With Python's __all__ (Course)
- Python Lazy Imports: Speed Up Startup With the lazy Keyword (Tutorial)
- Python's __all__: Packages, Modules, and Wildcard Imports (Quiz)
- Python import: Advanced Techniques and Tips (Quiz)
- Absolute vs Relative Imports in Python (Quiz)
- Python 3.15 Preview: Lazy Imports (Quiz)
Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.
By Leodanis Pozo Ramos • Updated Oct. 13, 2026