{"id":6287,"library":"types-pkg-resources","title":"Typing Stubs for pkg_resources","description":"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.","status":"active","version":"0.1.3","language":"en","source_language":"en","source_url":"https://github.com/python/typeshed/tree/main/stubs/pkg_resources","tags":["typing","type hints","stubs","pkg_resources","setuptools"],"install":[{"cmd":"pip install types-pkg-resources","lang":"bash","label":"Install only stubs (for type checking)"},{"cmd":"pip install types-pkg-resources setuptools","lang":"bash","label":"Install stubs and the runtime library"}],"dependencies":[{"reason":"The `pkg_resources` module is part of the `setuptools` distribution. These stubs are only useful if `setuptools` (and thus `pkg_resources`) is installed at runtime.","package":"setuptools","optional":false}],"imports":[{"note":"You import the runtime library `pkg_resources`. The `types-pkg-resources` package provides the type hints that type checkers use when analyzing this import.","symbol":"pkg_resources","correct":"import pkg_resources"},{"note":"Specific types like `Distribution` are imported directly from `pkg_resources`.","symbol":"Distribution","correct":"from pkg_resources import Distribution"}],"quickstart":{"code":"import pkg_resources\nfrom typing import List\n\n# Example function using pkg_resources to list installed packages\ndef get_installed_package_names() -> List[str]:\n    \"\"\"Returns a sorted list of project names for installed packages.\"\"\"\n    # pkg_resources.working_set provides access to the active distributions\n    distributions = pkg_resources.working_set\n    return sorted([d.project_name for d in distributions])\n\nif __name__ == \"__main__\":\n    print(\"--- Listing installed packages via pkg_resources ---\")\n    packages = get_installed_package_names()\n    print(f\"Found {len(packages)} packages. First 5:\")\n    for p in packages[:5]:\n        print(f\"- {p}\")\n\n# To type-check this code:\n# 1. Install necessary packages: `pip install types-pkg-resources setuptools mypy`\n# 2. Run mypy: `mypy your_script_name.py`","lang":"python","description":"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."},"warnings":[{"fix":"For new projects, prefer `importlib.metadata` (Python 3.8+) or `setuptools.metadata`. Only use `types-pkg-resources` if you are maintaining an existing codebase that relies on `pkg_resources`.","message":"The `pkg_resources` module, for which these stubs are provided, is considered a legacy API. For new Python projects (especially Python 3.8+), it's highly recommended to use `importlib.metadata` for package metadata access. For older Python versions, `setuptools.metadata` offers a more modern alternative within the setuptools ecosystem.","severity":"gotcha","affected_versions":"All versions of `pkg_resources` and `types-pkg-resources`."},{"fix":"Ensure `setuptools` is installed in your runtime environment alongside `types-pkg-resources` for development-time type checking. For example: `pip install setuptools types-pkg-resources`.","message":"Type stubs (`types-pkg-resources`) do not provide any runtime functionality. They are solely for static analysis by type checkers. Your project must have the actual `pkg_resources` module (typically bundled with `setuptools`) installed to run the code at runtime.","severity":"gotcha","affected_versions":"All versions."},{"fix":"When troubleshooting runtime issues related to `pkg_resources`, focus on the `setuptools` version. When troubleshooting type checking issues, refer to the `types-pkg-resources` version and the `typeshed` project.","message":"The version of `types-pkg-resources` tracks the evolution of the *stub definitions* for `pkg_resources`, not the `pkg_resources` module's actual version or functionality. The `pkg_resources` module's behavior is tied to the `setuptools` library version it's shipped with.","severity":"gotcha","affected_versions":"All versions."}],"env_vars":null,"last_verified":"2026-04-14T00:00:00.000Z","next_check":"2026-07-13T00:00:00.000Z","problems":[]}