Python 3 Bindings for NVIDIA Management Library (NVML)

0.2.7 · active · verified Fri Apr 17

py3nvml provides Python 3 bindings for the NVIDIA Management Library (NVML), allowing users to monitor and manage NVIDIA GPUs. It's built on the original pynvml project but specifically targets Python 3. The current version is 0.2.7, and releases are infrequent, primarily for maintenance or compatibility updates.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart initializes NVML, queries the number of available NVIDIA GPUs, iterates through them to print their name and memory usage, and finally shuts down NVML. It includes basic error handling for common NVML issues.

from pynvml import nvmlInit, nvmlShutdown, nvmlDeviceGetCount, nvmlDeviceGetHandleByIndex, nvmlDeviceGetName, nvmlDeviceGetMemoryInfo, NVMLError

try:
    nvmlInit()
    device_count = nvmlDeviceGetCount()
    print(f"Found {device_count} NVIDIA GPU(s).")

    for i in range(device_count):
        handle = nvmlDeviceGetHandleByIndex(i)
        name = nvmlDeviceGetName(handle)
        mem_info = nvmlDeviceGetMemoryInfo(handle)
        print(f"  Device {i}: {name} - Total Memory: {mem_info.total / (1024**3):.2f} GB, Used: {mem_info.used / (1024**3):.2f} GB")

except NVMLError as error:
    print(f"NVMLError: {error}")
    if error.value == 6: # NVML_ERROR_DRIVER_NOT_LOADED
        print("\n  Cause: NVIDIA drivers may not be installed or are outdated, or the NVML library cannot be found.")
        print("  Fix: Ensure NVIDIA display drivers are properly installed and the NVML shared library is accessible.")
    elif error.value == 10: # NVML_ERROR_NO_PERMISSION
        print("\n  Cause: Insufficient permissions to access NVIDIA GPU devices.")
        print("  Fix: Run the script with appropriate permissions (e.g., as root or with specific user group).")
except Exception as e:
    print(f"An unexpected error occurred: {e}")
finally:
    try:
        nvmlShutdown()
        print("NVML shutdown successfully.")
    except NVMLError as error:
        print(f"Error during NVML shutdown: {error}")

view raw JSON →