NVIDIA GPU Tools

0.10.0 · active · verified Fri Apr 17

nvgpu is a Python library providing tools for interacting with NVIDIA GPUs, offering functionalities to list GPUs, retrieve detailed information, and monitor their status. It acts as a user-friendly wrapper around the lower-level pynvml library. The current version is 0.10.0, and it maintains an active development pace with several releases per year.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart example demonstrates how to use nvgpu to list all detected NVIDIA GPUs and retrieve detailed information for a specific GPU. It includes basic error handling for environments without NVIDIA GPUs or proper driver setup.

import nvgpu
import os

# Check if NVIDIA GPU and drivers are likely available
# This is a basic check, actual NVML errors will still occur if setup is bad
if os.path.exists('/dev/nvidia0') or os.environ.get('CUDA_VISIBLE_DEVICES') is not None:
    try:
        # Get a list of all GPUs and their basic info
        gpus = nvgpu.list_gpus()
        print("Detected GPUs:")
        if gpus:
            for gpu_id, gpu_data in gpus.items():
                print(f"  GPU {gpu_id}:")
                for key, value in gpu_data.items():
                    print(f"    {key}: {value}")

            # Get detailed info for a specific GPU (e.g., the first one)
            first_gpu_id = list(gpus.keys())[0]
            detailed_info = nvgpu.gpu_info(gpu_id=first_gpu_id)
            print(f"\nDetailed info for GPU {first_gpu_id}:")
            for key, value in detailed_info.items():
                print(f"  {key}: {value}")
        else:
            print("No NVIDIA GPUs detected or NVML could not be initialized.")

    except Exception as e:
        print(f"An error occurred: {e}")
        print("Please ensure NVIDIA drivers are installed and nvidia-smi works.")
else:
    print("No NVIDIA GPUs detected or environment not configured for GPUs. Skipping nvgpu operations.")
    print("Ensure NVIDIA drivers are installed and CUDA_VISIBLE_DEVICES is set if in a restricted environment.")

view raw JSON →