On this page
pkgutil
Prev: pickletools | Up: stdlib_module | Next: platform
Rel: sys, sysconfig, builtins, warnings, dataclasses
Ref: docs.python.org — pkgutil
Tags: #public #python3 #stdlib
Import
import pkgutil
Overview
Package discovery and module-walking helpers (iter_modules, walk_packages, get_data) for enumerating submodules inside a Python package or across sys.path. For reading packaged data files, prefer importlib.resources.files() (in importlib); for third-party plugin discovery, prefer importlib.metadata.entry_points().
Quick Start
import json
import pkgutil
submodules = [m.name for m in pkgutil.iter_modules(json.__path__)]
assert "decoder" in submodules and "encoder" in submodules
Implementation Walkthrough
1. Discovering Submodules Without Importing Them (iter_modules)
iter_modules() inspects directories and zip finders without executing module code—unlike walk_packages(), which imports every parent package it traverses:
import email
import pkgutil
names = sorted(info.name for info in pkgutil.iter_modules(email.__path__, prefix="email."))
assert "email.message" in names
Gotchas & Boundaries
walk_packages()imports every package it descends into: That executes top-level code (which can have side effects or fail on optional dependencies); preferiter_modules()when passive discovery is enough.- Pass
onerror=lambda name: Nonetowalk_packages()if optional submodules might raiseImportError. - Prefer
importlib.resources.files()overpkgutil.get_data()for reading packaged assets in Python 3.9+.
Common Functions
- pkgutil.extend_path() —
extend_path(path, name)— Extend a package's path. - pkgutil.get_data() —
get_data(package, resource)— Get a resource from a package. - pkgutil.get_importer() —
get_importer(path_item)— Retrieve a finder for the given path item The returned finder is cached in sys.path_importer_cache if it was newly created by a path hook. - pkgutil.iter_importers() —
iter_importers(fullname='')— Yield finders for the given module name If fullname contains a '.', the finders will be for the package containing fullname, otherwise they will be all registered top level - pkgutil.iter_modules() —
iter_modules(path=None, prefix='')— Yields ModuleInfo for all submodules on path, or, if path is None, all top-level modules on sys.path. - pkgutil.read_code() —
read_code(stream)—pkgutil.read_code()function. - pkgutil.walk_packages() —
walk_packages(path=None, prefix='', onerror=None)— Yields ModuleInfo for all modules recursively on path, or, if path is None, all accessible modules.
Classes & Objects
- pkgutil.ModuleInfo — A namedtuple with minimal info about a module. (Methods:
.count(),.index())
Linked from
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/pickletools
- tuts/python3/stdlib_module/pkgutil/extend_path
- tuts/python3/stdlib_module/pkgutil/get_data
- tuts/python3/stdlib_module/pkgutil/get_importer
- tuts/python3/stdlib_module/pkgutil/iter_importers
- tuts/python3/stdlib_module/pkgutil/iter_modules
- tuts/python3/stdlib_module/pkgutil/ModuleInfo
- tuts/python3/stdlib_module/pkgutil/read_code
- tuts/python3/stdlib_module/pkgutil/walk_packages
- tuts/python3/stdlib_module/platform
- tuts/python3/stdlib_module/stdlib_module
Related notes
- tuts/python3/stdlib_module/pickletools Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/pkgutil/extend_path Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/pkgutil/get_data Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/pkgutil/get_importer Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/pkgutil/iter_importers Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/pkgutil/iter_modules Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/pkgutil/ModuleInfo Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/pkgutil/read_code Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/pkgutil/walk_packages Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/platform Linked here; links to this page; shared tags: #stdlib