Typing stubs for psutil

7.2.2.20260408 · active · verified Thu Apr 09

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.

Warnings

Install

Imports

Quickstart

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.

import psutil
from typing import Dict, Any

def get_process_info(pid: int) -> Dict[str, Any]:
    """Gets basic info for a process by PID."""
    try:
        process = psutil.Process(pid)
        return {
            "pid": process.pid,
            "name": process.name(),
            "cpu_percent": process.cpu_percent(interval=0.1),
            "memory_info_mb": process.memory_info().rss / (1024 * 1024) # MB
        }
    except psutil.NoSuchProcess:
        return {"error": f"Process {pid} not found."}
    except psutil.AccessDenied:
        return {"error": f"Access denied for process {pid}."}

if __name__ == "__main__":
    # Get info for current process
    current_process_info = get_process_info(psutil.Process().pid)
    print(f"Current process info: {current_process_info}")

    # To leverage `types-psutil`, install it and run a type checker:
    # pip install psutil types-psutil mypy
    # mypy your_script.py
    # MyPy will then correctly infer types for `process.name()`, `process.cpu_percent()`, etc.

view raw JSON →