Python Utilities for the NVIDIA Management Library

13.0.1 · deprecated · verified Thu Apr 09

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

Install

Imports

Quickstart

This quickstart demonstrates how to initialize NVML, query the number of available GPUs, retrieve device-specific information (name, memory, temperature), and properly shut down the NVML library. Error handling for common NVML issues is included.

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

view raw JSON →