{"id":1436,"library":"cuda-python","title":"CUDA Python","description":"CUDA Python provides a high-performance Python interface to NVIDIA's CUDA Driver and Runtime APIs, allowing direct GPU programming from Python. It bridges Python applications with CUDA-enabled GPUs, enabling GPU acceleration for custom kernels and integration with other CUDA libraries. The current version is 13.2.0 and releases generally align with major CUDA Toolkit updates.","status":"active","version":"13.2.0","language":"en","source_language":"en","source_url":"https://github.com/NVIDIA/cuda-python","tags":["cuda","gpu","nvidia","deep-learning","high-performance","driver-api","runtime-api"],"install":[{"cmd":"pip install cuda-python","lang":"bash","label":"Install cuda-python"}],"dependencies":[],"imports":[{"note":"For direct access to the low-level CUDA Driver API.","symbol":"cuda_driver","correct":"import cuda.cuda_driver as drv"},{"note":"For access to the CUDA C Runtime API wrappers.","symbol":"cuda_runtime","correct":"import cuda.cuda_runtime as rt"},{"note":"An alternative import for the CUDA C Runtime API, often used for compatibility with other libraries.","symbol":"cudart","correct":"from cuda import cudart"}],"quickstart":{"code":"import cuda.cuda_driver as drv\n\ntry:\n    # Initialize the CUDA driver API\n    # The '0' indicates the flags for initialization, 0 means default.\n    drv.cuInit(0)\n\n    # Get the number of available CUDA devices\n    err, device_count = drv.cuDeviceGetCount()\n\n    if err == drv.CUresult.CUDA_SUCCESS:\n        print(f\"Successfully initialized CUDA. Found {device_count} CUDA devices.\")\n        for i in range(device_count):\n            err, device = drv.cuDeviceGet(i)\n            if err == drv.CUresult.CUDA_SUCCESS:\n                # Get device name (256 is max length)\n                err, name_bytes = drv.cuDeviceGetName(256, device)\n                if err == drv.CUresult.CUDA_SUCCESS:\n                    # Decode the bytes to string and strip null terminators\n                    device_name = name_bytes.decode('utf-8').strip('\\x00')\n                    print(f\"  Device {i}: {device_name}\")\n    else:\n        print(f\"Failed to get CUDA device count. Error: {err.name}\")\nexcept drv.CUError as e:\n    print(f\"A CUDA driver error occurred: {e}. Ensure CUDA Toolkit and drivers are installed correctly and compatible.\")\nexcept Exception as e:\n    print(f\"An unexpected error occurred: {e}\")","lang":"python","description":"This quickstart initializes the CUDA Driver API and queries the number of available CUDA-enabled GPUs on the system, printing their names. It demonstrates basic interaction with the driver API and includes error handling for common CUDA-related issues."},"warnings":[{"fix":"Ensure you have the appropriate NVIDIA GPU drivers and a compatible CUDA Toolkit installed on your system. Refer to NVIDIA's documentation for installation instructions.","message":"The `cuda-python` package itself does NOT install the CUDA Toolkit or NVIDIA drivers. These are system-level prerequisites that must be installed separately and be compatible with your GPU. Installing `cuda-python` via pip only provides the Python bindings.","severity":"breaking","affected_versions":"All versions"},{"fix":"Try to align the `cuda-python` package's major version with your installed CUDA Toolkit's major version (e.g., `pip install cuda-python==12.x`). Check the `cuda-python` documentation for recommended compatibility matrix.","message":"Compatibility between `cuda-python` package version and the system's CUDA Toolkit version is crucial. While minor version mismatches might work, major version mismatches (e.g., `cuda-python==12.x` with CUDA Toolkit 11.x) are likely to cause `ImportError` or runtime errors.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Consult the official `cuda-python` documentation to understand the differences between the Driver and Runtime APIs and select the interface best suited for your application.","message":"The library exposes multiple API interfaces (e.g., `cuda.cuda_driver` for Driver API, `cuda.cuda_runtime` or `from cuda import cudart` for Runtime API). Choosing the correct API for your specific task (e.g., low-level control vs. higher-level abstractions, or integration with other libraries like Numba/PyTorch) is important.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always pair `drv.cuMemAlloc` with a corresponding `drv.cuMemFree` call, ideally within a `try...finally` block or by using context managers if available for robust resource management.","message":"When using the low-level CUDA Driver API (`cuda.cuda_driver`), memory allocation and deallocation on the GPU (e.g., `drv.cuMemAlloc`, `drv.cuMemFree`) must be managed manually. Forgetting to free allocated memory can lead to GPU memory leaks and resource exhaustion.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}