NVIDIA Management Library Python Bindings
nvidia-ml-py provides official Python bindings for the NVIDIA Management Library (NVML), enabling programmatic access to NVIDIA GPU monitoring and management functions. It wraps the NVML C shared library, which is typically distributed with NVIDIA graphics drivers. The library is actively maintained with frequent updates, as indicated by its high versioning scheme.
Warnings
- breaking The library often breaks backward compatibility without comprehensive changelogs. Changes to underlying NVML structures (e.g., `nvmlProcessInfo_t` with CUDA 11+) can cause `FunctionNotFound` errors or incorrect results with older NVIDIA drivers.
- gotcha NVML initialization errors (`NVMLError_V1_UNINITIALIZED` or similar) are common due to outdated/missing NVIDIA drivers, misconfigured CUDA, permission issues, or conflicting driver versions.
- gotcha There can be confusion between `nvidia-ml-py` (official bindings) and other community-maintained packages like `pynvml` (which is deprecating its own bindings in favor of `nvidia-ml-py`) or the deprecated `nvidia-ml-py3`.
- gotcha Some constants within `pynvml.py` (e.g., `NVML_P2P_CAPS_INDEX_READ`) may be incorrectly defined as tuples instead of integers due to extraneous commas, leading to unexpected behavior.
- gotcha Some specific function bindings, such as `nvmlDeviceGetGpcClkMinMaxVfOffset` and `nvmlDeviceGetMemClkMinMaxVfOffset`, may incorrectly expect more parameters than required by the underlying C function, leading to argument errors.
Install
-
pip install nvidia-ml-py
Imports
- All NVML functions and constants
from pynvml import *
Quickstart
from pynvml import *
try:
nvmlInit()
print(f"Driver Version: {nvmlSystemGetDriverVersion()}")
deviceCount = nvmlDeviceGetCount()
for i in range(deviceCount):
handle = nvmlDeviceGetHandleByIndex(i)
print(f"Device {i}: {nvmlDeviceGetName(handle)}")
# Example: Get memory info
info = nvmlDeviceGetMemoryInfo(handle)
print(f" Total Memory: {info.total / (1024**3):.2f} GB")
print(f" Used Memory: {info.used / (1024**3):.2f} GB")
print(f" Free Memory: {info.free / (1024**3):.2f} GB")
except NVMLError as error:
print(f"NVML Error: {error}")
finally:
try:
nvmlShutdown()
except NVMLError as error:
print(f"NVML Shutdown Error: {error}")