On this page
importlib
Prev: imaplib | Up: stdlib_module | Next: inspect
Rel: sys, sysconfig, builtins, warnings, dataclasses
Ref: docs.python.org — importlib
Tags: #public #python3 #stdlib
Import
import importlib
Overview
Programmatic interface to Python's import system (import_module), installed package metadata and entry points (importlib.metadata), and bundled non-code package assets (importlib.resources). Reach for importlib.metadata for plugin discovery and version lookup, and importlib.resources instead of fragile __file__ path hacks for reading packaged templates or schemas.
Quick Start
import importlib
from importlib import metadata
math_mod = importlib.import_module("math")
assert math_mod.sqrt(16) == 4.0
# Discover installed entry points for a plugin group:
eps = metadata.entry_points(group="console_scripts")
assert hasattr(eps, "select")
Implementation Walkthrough
1. Reading Packaged Data Files with importlib.resources
Works cleanly across editable installs, wheels, and zipapps without relying on __file__:
from importlib import resources
def read_packaged_text(package: str, filename: str) -> str:
return resources.files(package).joinpath(filename).read_text(encoding="utf-8")
2. Explicit Plugin Discovery via importlib.metadata.entry_points
from importlib import metadata
def list_plugins(group: str) -> dict[str, str]:
return {ep.name: ep.value for ep in metadata.entry_points(group=group)}
Gotchas & Boundaries
- Never pass untrusted user input straight to
import_module(): Importing a module executes its top-level code; validate against an explicit allowlist first. importlib.reload()does NOT update existing references: Objects imported viafrom mod import fnkeep pointing to the old function even afterreload(mod).metadata.version("pkg-name")uses the distribution name (oftenkebab-case), not the Python import name (snake_case).- Prefer
resources.files(pkg)(Python 3.9+) over deprecatedread_text(pkg, name)helpers.
Common Functions
- importlib.import_module() —
import_module(name, package=None)— Import a module. - importlib.invalidate_caches() —
invalidate_caches()— Call the invalidate_caches() method on all meta path finders stored in sys.meta_path (where implemented). - importlib.reload() —
reload(module)— Reload the module and return it.
Linked from
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/imaplib
- tuts/python3/stdlib_module/importlib/import_module
- tuts/python3/stdlib_module/importlib/invalidate_caches
- tuts/python3/stdlib_module/importlib/reload
- tuts/python3/stdlib_module/inspect
- tuts/python3/stdlib_module/pkgutil
- tuts/python3/stdlib_module/stdlib_module
- tuts/python3/tutorials/intermediate_python_and_software_engineering/modules_packages_and_tooling
Related notes
- tuts/python3/stdlib_module/imaplib Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/importlib/import_module Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/importlib/invalidate_caches Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/importlib/reload Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/inspect Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/stdlib_module Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/builtins Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/dataclasses Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/pkgutil links to this page; shared tags: #stdlib