psutil is a cross-platform library for retrieving information on running processes and system utilization (CPU, memory, disks, network, sensors) in Python. It is useful for system monitoring, profiling, limiting process resources, and managing running processes. The current version is 8.0.0, released on March 28, 2026, with a regular release cadence.
pip install psutilVerified import paths — ran on the pinned version, not inferred.
This script demonstrates how to retrieve CPU times and list all running processes using psutil.
Replace 'psutil.OSX' with 'psutil.MACOS' in your code.
Install psutil using 'pip install psutil'.
Run `pip install psutil` in your terminal to install the library. If using a virtual environment, ensure it's activated.
Wrap the problematic `psutil` calls in a `try-except psutil.AccessDenied:` block to gracefully skip processes you don't have access to, or run the script with elevated privileges (e.g., `sudo python your_script.py` on Linux/macOS or 'Run as Administrator' on Windows).
Always enclose operations on `psutil.Process` objects, especially when iterating through processes, within a `try-except psutil.NoSuchProcess:` block to handle cases where a process disappears.
Call `psutil.cpu_percent()` (or `Process.cpu_percent()`) twice with a short time interval (e.g., 0.1 to 1 second) in between. Discard the result of the first call as it's a meaningless baseline. Example: `psutil.cpu_percent(interval=None)` then `time.sleep(0.1)` then `psutil.cpu_percent(interval=None)`.
Ensure `psutil` is updated to the latest version (`pip install --upgrade psutil`) and verify the attribute/method name against the official psutil documentation. Also, check if there's any file named `psutil.py` in your project directory that might be conflicting with the installed library.