Skip to main content

pkgutil — Package extension utility

Official Documentation

Overview

pkgutil provides utilities for the import system, including functions for finding, loading, and iterating over Python packages and modules. It is commonly used for plugin systems, namespace packages, and dynamic module discovery.


Module Classification: Simple

  • Reason: pkgutil contains primarily functions and a small number of classes (less than 3 user-facing classes).
  • Documentation strategy: Single file (pkgutil.md) covering all functions, classes, and constants.

Attribute Inventory

NameTypeDescription
extend_pathfunctionExtend the search path for modules/packages.
find_loaderfunctionFind the loader for a module.
get_datafunctionGet a resource from a package.
get_importerfunctionGet the importer for a path.
iter_importersfunctionIterate over importers for a path.
iter_modulesfunctionIterate over modules in a package.
walk_packagesfunctionRecursively walk packages and yield module info.
ModuleInfoclassNamed tuple for module info.
ImpImporterclassDeprecated: Importer for the import machinery.
ImpLoaderclassDeprecated: Loader for the import machinery.

Quick Reference Table

Function/ClassSignatureDescription
extend_pathextend_path(path, name)Extend package path for PEP 302 and PEP 420 support.
find_loaderfind_loader(fullname, path=None)Find loader for module (deprecated in 3.4+).
get_dataget_data(package, resource)Get resource bytes from a package.
get_importerget_importer(path_item)Return importer for a path item.
iter_importersiter_importers(fullname='')Yield importers for a module/package.
iter_modulesiter_modules(path=None, prefix='')Yield (module_finder, name, ispkg) for modules.
walk_packageswalk_packages(path=None, prefix='', onerror=None)Recursively yield (module_finder, name, ispkg).
ModuleInfoclass ModuleInfo(module_finder, name, ispkg)Info about a discovered module.
ImpImporterclass ImpImporter(path_item)Deprecated: Importer for legacy import system.
ImpLoaderclass ImpLoader(fullname, file, filename, etc.)Deprecated: Loader for legacy import system.

Practical Use Cases

1. Iterating Over All Modules in a Package

import pkgutil
for finder, name, ispkg in pkgutil.iter_modules():
print(f"{name} (Package: {ispkg})")

2. Walking Packages Recursively

import pkgutil
for finder, name, ispkg in pkgutil.walk_packages(path=None, prefix='myproject.'):
print(f"Discovered: {name} (Package: {ispkg})")

3. Loading Data from a Package Resource

import pkgutil
# Suppose 'data.txt' is inside the 'mypkg' package
content = pkgutil.get_data('mypkg', 'data.txt')
print(content.decode())

4. Extending a Package Path (Namespace Packages)

import pkgutil
__path__ = pkgutil.extend_path(__path__, __name__)

Performance Notes

  • Functions like iter_modules and walk_packages are efficient for module discovery but may be slow on very large package trees.
  • Deprecated classes (ImpImporter, ImpLoader) should not be used in new code.

Further Reading


Source Attribution