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.
pip install nvidia-ml-pyVerified import paths — ran on the pinned version, not inferred.
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.
Refer to NVIDIA's NVML documentation for driver-specific API changes. Test thoroughly after driver/CUDA updates or library upgrades. Consider pinning specific `nvidia-ml-py` versions in your `requirements.txt`.
Ensure NVIDIA drivers are up-to-date and compatible with your GPU. Verify CUDA toolkit installation. Check user permissions for GPU access. Restart Python kernel/script after making system changes. Consult NVIDIA's documentation for specific driver/NVML compatibility.
Always `pip install nvidia-ml-py` for the official, actively maintained bindings. Use `from pynvml import *` for imports, as `pynvml` is the module name within the `nvidia-ml-py` package.
Manually inspect the `pynvml.py` source for affected constants if you encounter type-related errors. If possible, upgrade to a version where this is known to be resolved, or implement workarounds in your code.
Consult the exact function signature in the NVIDIA NVML C documentation and compare it with the Python binding's behavior. If an inconsistency is found, you might need to adjust your function call or be aware of potential issues.
Ensure NVIDIA drivers are correctly installed and loaded. On Linux, you might need to reboot or reinstall drivers. Verify `nvidia-smi` works. If running in a container, ensure the necessary driver volumes are mounted or that the container runtime (e.g., `nvidia-container-runtime`) is correctly configured.
Check if your GPU hardware supports the specific NVML feature you are trying to access. Update your NVIDIA drivers to the latest version. For very new hardware (like Grace Blackwell), there might be specific shim libraries or workarounds required.
The correct way to interact with NVML is usually through `pynvml` directly after importing it, for example: `from pynvml import *` or `import pynvml` and then calling functions like `pynvml.nvmlInit()`.
Update your `nvidia-ml-py` library to a version compatible with your NVIDIA driver. For example, if you have a specific driver version (e.g., 535.104.05), you might need `nvidia-ml-py:12.535.78+`.