{"id":1760,"library":"types-psutil","title":"Typing stubs for psutil","description":"The `types-psutil` package provides PEP 561-compliant typing stubs for the `psutil` library, enabling static type checkers like MyPy and Pyright to analyze code that uses `psutil` for system and process utilities. As part of the typeshed project, its release cadence is tied to typeshed's updates and the `psutil` library's own releases, ensuring up-to-date type coverage. The current version is 7.2.2.20260408.","status":"active","version":"7.2.2.20260408","language":"en","source_language":"en","source_url":"https://github.com/python/typeshed","tags":["typing","stubs","type-hints","psutil","typeshed"],"install":[{"cmd":"pip install types-psutil","lang":"bash","label":"Install `types-psutil`"}],"dependencies":[{"reason":"This library provides typing stubs for the `psutil` library; `psutil` itself must be installed for runtime functionality and for type checking to be meaningful.","package":"psutil","optional":false}],"imports":[{"note":"The `types-psutil` library provides type stubs for the `psutil` library, enabling static analysis tools like MyPy to check code using `psutil`. It does not contain any runtime code or symbols intended for direct import by users. Your runtime code should only import from `psutil`.","symbol":"No direct imports from types-psutil","correct":"Users import symbols from `psutil` (e.g., `from psutil import cpu_percent`) after installing `types-psutil` for type checking."}],"quickstart":{"code":"import psutil\nfrom typing import Dict, Any\n\ndef get_process_info(pid: int) -> Dict[str, Any]:\n    \"\"\"Gets basic info for a process by PID.\"\"\"\n    try:\n        process = psutil.Process(pid)\n        return {\n            \"pid\": process.pid,\n            \"name\": process.name(),\n            \"cpu_percent\": process.cpu_percent(interval=0.1),\n            \"memory_info_mb\": process.memory_info().rss / (1024 * 1024) # MB\n        }\n    except psutil.NoSuchProcess:\n        return {\"error\": f\"Process {pid} not found.\"}\n    except psutil.AccessDenied:\n        return {\"error\": f\"Access denied for process {pid}.\"}\n\nif __name__ == \"__main__\":\n    # Get info for current process\n    current_process_info = get_process_info(psutil.Process().pid)\n    print(f\"Current process info: {current_process_info}\")\n\n    # To leverage `types-psutil`, install it and run a type checker:\n    # pip install psutil types-psutil mypy\n    # mypy your_script.py\n    # MyPy will then correctly infer types for `process.name()`, `process.cpu_percent()`, etc.","lang":"python","description":"This quickstart demonstrates how to use the `psutil` library. By installing `types-psutil`, static type checkers will be able to provide accurate type hints and catch potential type-related errors in your `psutil` code. The example retrieves and displays information about the current running process."},"warnings":[{"fix":"Ensure both `psutil` and `types-psutil` are installed: `pip install psutil types-psutil`","message":"The `types-psutil` library provides type hints for the `psutil` library but does not install `psutil` itself. For your code to run and for type checking to be effective, `psutil` must also be explicitly installed in your environment.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Refer to the `typeshed` repository or `psutil` documentation for compatibility notes. Upgrade `types-psutil` regularly to match your `psutil` version.","message":"As `types-psutil` is part of `typeshed`, its versioning includes a date component (e.g., `7.2.2.20260408`). While it generally tracks the `psutil` version it provides stubs for, it's possible for there to be a mismatch. Ensure the `psutil` version you are using is compatible with the installed `types-psutil` stubs to avoid partial or incorrect type coverage, especially with very new `psutil` releases.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Only import symbols from the actual `psutil` library in your runtime code. `types-psutil` is installed to aid your static analysis tools, not for runtime imports.","message":"Typing stubs like `types-psutil` are purely for static analysis tools (e.g., MyPy, Pyright). They do not add any runtime functionality or modify the behavior of `psutil`. Do not attempt to import symbols directly from `types_psutil` or expect it to change runtime behavior; it only influences how type checkers interpret your `psutil` usage.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}