Python Glossary Русский

all Python 3Modules & importsterm

pkgutil

module

Utilities for packages: list the modules inside a package and read a data file shipped with it.

Syntax

import pkgutil

Today the main use of pkgutil is iter_modules and walk_packages: they list a package's modules without importing them, which suits plugin systems. Reading package resources (get_data) and finding loaders is done in new code with importlib.resources and importlib.util; find_loader and get_loader were deprecated and removed in 3.14. Unlike iter_modules, walk_packages imports subpackages to look inside them.

Examples

import pkgutil, pathlib
pkg = pathlib.Path('plugins')
pkg.mkdir(exist_ok=True)
for name in ('alpha', 'beta'):
    (pkg / f'{name}.py').write_text('', encoding='utf-8')
print(sorted(m.name for m in pkgutil.iter_modules(['plugins'])))  # → ['alpha', 'beta']

See also

Open in the full glossarydocs.python.orgReport an inaccuracy