pkgutil
It is a module in Python's standard library that provides a series of package-related tool functions, such as dynamically loading packages, recursively traversing submodules in packages, and so on. In this tutorial, we will take you to learn more about pkgutil
the main functions of the module and how to use it.
1 Introduction
pkgutil
A module is a tool module in the Python standard library, which provides a series of package-related functional functions. Commonly used functions include:
iter_modules
: Traversing all modules in a package (including subpackages) in the form of an iterator.get_data
: Get specified data (such as pictures, configuration files, etc.) in a package or module.find_loader
: Returns a Loader object of a package or module, which is used to dynamically load the package or module.walk_packages
: Recursively traverse all non-subpackage modules in a package in the form of an iterator.extend_path
: extension module lookup path, used to support namespace packages.
Below we describe these functions in detail.
2. Common functions
2.1 iter_modules
iter_modules
The function is used to traverse all modules (including subpackages) in a package as an iterator. Its syntax is as follows:
pkgutil.iter_modules(path=None, prefix='')
Among them, path
the parameter specifies the path (string or list of strings) of the package to traverse, and defaults to if not specified sys.path
. prefix
The parameter is used to specify the prefix of the module name, for example, prefix='mypackage.'
only return modules prefixed mypackage.
with .
iter_modules
The function returns an iterator, one tuple per iteration (loader, name, ispkg)
, where:
loader
Is a Loader object that can be used to load the module.name
is the full name of the module, such asmymodule
ormypackage.subpackage.mymodule
ispkg
Indicates whether the module is a subpackage.
The following code shows how to use iter_modules
the function:
import pkgutil
for loader, module_name, is_pkg in pkgutil.iter_modules():
print(f"Module: {module_name}, is package: {is_pkg}")
2.2 get_data
get_data
The function is used to obtain the specified data (such as pictures, configuration files, etc.) in a package or module. Its syntax is as follows:
pkgutil.get_data(package, resource_name)
Among them, package
the parameter specifies the package or module to obtain data, resource_name
and specifies the name of the data to obtain.
The following code shows how to use get_data
a function to get the data of an image resource in a package:
import pkgutil
data = pkgutil.get_data('mypackage', 'logo.png')
with open('logo.png', 'wb') as f:
f.write(data)
2.3 find_loader
find_loader
The function is used to return a Loader object of a package or module, which is used to dynamically load the package or module. Its syntax is as follows:
pkgutil.find_loader(module_name)
where module_name
the parameter specifies the full name of the module to look for.
find_loader
The function returns a Loader object through which the package or module can be loaded.
The following code shows how to use find_loader
a function to get a module's Loader object and use it to load the module:
import pkgutil
loader = pkgutil.find_loader('mymodule')
module = loader.load_module()
2.4 walk_packages
walk_packages
Functions are a iter_modules
higher-level wrapper over functions that recursively traverse all non-subpackage modules in a package in the form of iterators. Its syntax is as follows:
pkgutil.walk_packages(path=None, prefix='', onerror=None, ispackage=None)
Among them, path
the parameter specifies the path (string or list of strings) of the package to traverse, and defaults to if not specified sys.path
. prefix
The parameter is used to specify the prefix of the module name, for example, prefix='mypackage.'
only return modules prefixed mypackage.
with . onerror
and ispackage
parameters are used to specify the error handling method and whether to return only subpackages.
walk_packages
The function returns an iterator, one tuple per iteration (loader, name, ispkg)
, where:
loader
Is a Loader object that can be used to load the module.name
is the full name of the module, such asmymodule
ormypackage.subpackage.mymodule
ispkg
Indicates whether the module is a subpackage.
The following code shows how to use walk_packages
a function to recursively traverse all non-subpackage modules in a package:
import pkgutil
for loader, module_name, is_pkg in pkgutil.walk_packages():
print(f"Module: {module_name}, is package: {is_pkg}")
2.5 extend_path
extend_path
Function to extend the module lookup path to support namespace packages. Its syntax is as follows:
pkgutil.extend_path(path, name)
Among them, path
the parameter should be a list, which stores the module search path to be extended. name
The argument specifies the name of the namespace package to extend.
extend_path
The function will return a new list of paths containing the paths name
for modules that are namespaced. In this way, when Python's import statement encounters name
a module name beginning with , the module can be loaded correctly.
The following code shows how to use extend_path
functions to extend the module search path and support namespace packages:
import pkgutil
pkg_path = ['myapp/']
pkgutil.extend_path(pkg_path, 'myapp.namespace')
3. Summary
This article introduces pkgutil
the modules in the Python standard library, including how to use common functions iter_modules
, get_data
, find_loader
, walk_packages
, etc. Module is a very useful tool module in Python development. It can easily implement tasks such as package management, dynamic loading, and module traversal. Later, we will analyze the usage of this module in combination with the packaging scene of pyinstaller.extend_path
pkgutil