importlib (Python 2.7 Backport)
This `importlib` package on PyPI is a backport of the `importlib.import_module()` function from Python 2.7. It was created to provide this functionality for Python versions prior to 2.7 and Python 3.0. This project has been archived since November 2017 and is no longer maintained, as the functionality it backported is standard in all currently supported Python versions. It is *not* the standard library `importlib` module fundamental to Python 3's import system.
Warnings
- breaking This PyPI `importlib` package is a backport specifically for Python versions prior to 2.7 and for Python 3.0. It is completely obsolete and unnecessary for Python 2.7+ and Python 3.1+ where `importlib` is a standard library module. Installing it on modern Python 3 environments provides no benefit and can lead to confusion.
- deprecated The `importlib` PyPI project has been explicitly archived by its maintainer since November 2017 and is no longer under maintenance. No new releases, bug fixes, or security updates will be provided.
- gotcha There is significant confusion between the PyPI `importlib` package (this backport) and the `importlib` module in Python's standard library. The standard library `importlib` is a powerful, actively developed module in all Python 3 versions, offering extensive functionality for programmatic importing, module metadata (`importlib.metadata`), and resource access (`importlib.resources`). This PyPI package only backports a tiny fraction of that functionality.
- gotcha When performing dynamic imports, be mindful of relative imports. If `importlib.import_module()` is used with a relative name (e.g., `.mymodule`), the `package` argument must be provided as the anchor package name, otherwise an `ImportError` or `ModuleNotFoundError` may occur.
- deprecated For complex dynamic loading scenarios in Python 3, `importlib.find_loader()` is deprecated since Python 3.4. It should be replaced with `importlib.util.find_spec()`. Similarly, `loader.load_module()` is deprecated since 3.4 and will be removed in 3.15; use `loader.exec_module()` instead.
Install
-
pip install importlib
Imports
- import_module
from importlib import import_module
import importlib module = importlib.import_module('my_module')
Quickstart
import importlib
# This PyPI package primarily provides importlib.import_module
# for older Python 2.x environments where it wasn't built-in.
# For Python 3, 'importlib' is a standard library module.
try:
# Attempt to dynamically import a module
math_module = importlib.import_module('math')
print(f"Successfully imported math module. sqrt(16) = {math_module.sqrt(16)}")
except ImportError as e:
print(f"Could not import module: {e}")
print("Note: This 'importlib' PyPI package is a Python 2.7 backport and is obsolete for modern Python 3.x.")