NVIDIA Management Library Python Bindings

13.595.45 · active · verified Sun Apr 05

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

Install

Imports

Quickstart

This quickstart demonstrates how to initialize NVML, retrieve the NVIDIA driver version, enumerate available GPUs, and query basic information like device name and memory usage. It includes error handling for NVML specific errors and ensures NVML is shut down properly.

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}")

view raw JSON →