Python Utilities for the NVIDIA Management Library
pynvml is a Python library that provides utilities for the NVIDIA Management Library (NVML). As of version 13.0.1, this project (gpuopenanalytics/pynvml) itself is deprecated, primarily serving as a wrapper that pins to the official `nvidia-ml-py` bindings. It offers a Python interface for GPU management and monitoring functions, with the core NVML functionality now sourced from `nvidia-ml-py`. The current version is 13.0.1, and the project indicates an inactive development status.
Warnings
- breaking The `pynvml` project (gpuopenanalytics/pynvml) and its `pynvml_utils` module are officially deprecated as of version 13.0.1. Users are strongly advised to transition to using the `nvidia-ml-py` package directly for future development and stability.
- gotcha The NVIDIA Management Library (NVML) must be explicitly initialized with `pynvml.nvmlInit()` and shut down with `pynvml.nvmlShutdown()`. Failing to call `nvmlShutdown()` can lead to resource leaks and prevent subsequent NVML operations.
- gotcha The `nvidia-ml-py` library (which `pynvml` now wraps) has a history of breaking backward compatibility with often uncomprehensive changelogs, making migrations challenging.
- gotcha Many NVML functions require elevated privileges (e.g., admin/root) to query certain GPU performance counters or to modify GPU settings. Running without sufficient permissions can result in `NVMLError` exceptions.
- gotcha NVML functions in `pynvml` do not return error codes directly but instead raise Python exceptions (specifically `pynvml.NVMLError` and its subclasses) for failures.
Install
-
pip install pynvml
Imports
- pynvml
import pynvml
Quickstart
import pynvml
try:
pynvml.nvmlInit()
print(f"Driver Version: {pynvml.nvmlSystemGetDriverVersion()}")
device_count = pynvml.nvmlDeviceGetCount()
print(f"Found {device_count} GPU device(s).")
for i in range(device_count):
handle = pynvml.nvmlDeviceGetHandleByIndex(i)
device_name = pynvml.nvmlDeviceGetName(handle)
memory_info = pynvml.nvmlDeviceGetMemoryInfo(handle)
temperature = pynvml.nvmlDeviceGetTemperature(handle, pynvml.NVML_TEMPERATURE_GPU)
print(f"\nDevice {i} ({device_name.decode('utf-8')}):")
print(f" Total Memory: {memory_info.total / (1024**3):.2f} GB")
print(f" Used Memory: {memory_info.used / (1024**3):.2f} GB")
print(f" Free Memory: {memory_info.free / (1024**3):.2f} GB")
print(f" GPU Temperature: {temperature}°C")
except pynvml.NVMLError as error:
print(f"NVML Error: {error}")
except Exception as e:
print(f"An unexpected error occurred: {e}")
finally:
try:
pynvml.nvmlShutdown()
except pynvml.NVMLError_Uninitialized:
pass # NVML was not initialized or already shut down