NVIDIA Management Library Python Bindings (Python 3 Port)
The `nvidia-ml-py3` library provides Python 3 compatible bindings to the NVIDIA Management Library (NVML), a C-based API for monitoring and managing NVIDIA GPUs. It allows Python applications to query GPU statistics, health, and other operational data. This specific package is an older port for Python 3 from the original `nvidia-ml-py` and is currently at version 7.352.0. The project's GitHub repository indicates it is archived and recommends migrating to the actively maintained `nvidia-ml-py` package.
Warnings
- breaking The `nvidia-ml-py3` package is officially deprecated and its GitHub repository is archived. Users are strongly advised to switch to the actively maintained `nvidia-ml-py` package (note the lack of '3' in the name) for continued support and updates.
- gotcha NVML functionality critically depends on a correctly installed and up-to-date NVIDIA GPU driver. Issues like 'NVML not initialized' or 'NVMLError: Driver Not Loaded' typically indicate driver problems, missing driver files, or insufficient user permissions to access GPU devices.
- gotcha It is essential to call `pynvml.nvmlInit()` before any other NVML functions and `pynvml.nvmlShutdown()` when your application finishes. Failing to call `nvmlInit()` will result in `NVMLError: Uninitialized`, and not calling `nvmlShutdown()` can lead to resource leaks.
- breaking The underlying `nvidia-ml-py` (and thus `nvidia-ml-py3`) has historically introduced backward-incompatible API changes, particularly concerning data structures like `nvmlProcessInfo_t`. Newer versions might introduce fields (e.g., `gpuInstanceId`, `computeInstanceId`) that are incompatible with older NVIDIA drivers, causing crashes or incorrect data.
- gotcha Some constant definitions in `pynvml.py` (e.g., `NVML_P2P_CAPS_INDEX_READ`, `NVML_GRID_LICENSE_EXPIRY_` definitions) have been reported to contain extraneous commas, inadvertently turning them into tuples instead of integers. This can cause unexpected type errors if used directly.
Install
-
pip install nvidia-ml-py3
Imports
- pynvml
import pynvml
Quickstart
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)
name = pynvml.nvmlDeviceGetName(handle)
temperature = pynvml.nvmlDeviceGetTemperature(handle, pynvml.NVML_TEMPERATURE_GPU)
memory_info = pynvml.nvmlDeviceGetMemoryInfo(handle)
print(f" Device {i}: {name.decode('utf-8') if isinstance(name, bytes) else name}")
print(f" Temperature: {temperature}°C")
print(f" Memory: {memory_info.used >> 20}MiB / {memory_info.total >> 20}MiB (Used/Total)")
except pynvml.NVMLError as error:
print(f"NVML Error: {error}")
print("Common causes: Missing/outdated NVIDIA drivers, permission issues, or no GPUs found.")
finally:
try:
pynvml.nvmlShutdown()
except pynvml.NVMLError as error:
print(f"NVML Shutdown Error: {error}")