Install & Compatibility
Where this runs
tested against v0.2.7 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.910 runs
installs and imports cleanly · install 0.0s · import 0.000s · 18.4MB
glibcpy 3.10–3.910 runs
installs and imports cleanly · install 1.6s · import 0.000s · 19MB
16MB installed
● package 16MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
get_free_gpus
✓ from py3nvml import get_free_gpus
✗ from pynvml import nvmlInit
grab_gpus
✓ from py3nvml import grab_gpus
✗ from pynvml import nvmlInit
nvidia_smi
✓ from py3nvml import nvidia_smi
✗ from pynvml import nvmlInit
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}")
Upgrade
Version history
0.2.7latest on PyPI · released Nov 22, 2021
Audit
Dependencies
No dependency data recorded yet.