Typing Stubs for pkg_resources

0.1.3 · active · verified Tue Apr 14

This library provides static type checking stubs for the `pkg_resources` module, which is primarily distributed as part of `setuptools`. It enables type checkers like MyPy and pyright to analyze code using `pkg_resources` for package metadata and resource management. While `pkg_resources` itself is considered a legacy API, these stubs are actively maintained as part of the `typeshed` project to support existing Python codebases. New versions are released periodically as stub definitions are updated or improved.

Warnings

Install

Imports

Quickstart

This example demonstrates how to use `pkg_resources` to list installed packages and how `types-pkg-resources` provides type hints for this usage. For type checking to work, both `types-pkg-resources` and `setuptools` (which contains `pkg_resources`) must be installed in your environment, along with a type checker like MyPy.

import pkg_resources
from typing import List

# Example function using pkg_resources to list installed packages
def get_installed_package_names() -> List[str]:
    """Returns a sorted list of project names for installed packages."""
    # pkg_resources.working_set provides access to the active distributions
    distributions = pkg_resources.working_set
    return sorted([d.project_name for d in distributions])

if __name__ == "__main__":
    print("--- Listing installed packages via pkg_resources ---")
    packages = get_installed_package_names()
    print(f"Found {len(packages)} packages. First 5:")
    for p in packages[:5]:
        print(f"- {p}")

# To type-check this code:
# 1. Install necessary packages: `pip install types-pkg-resources setuptools mypy`
# 2. Run mypy: `mypy your_script_name.py`

view raw JSON →