Python中CUDA启用失败求助:NUMBA_DISABLE_CUDA=1环境变量问题
Hey there, let's work through this CUDA issue you're facing with Numba—super frustrating when you've followed all the setup steps and still hit errors, right? Let's break down the problem and tackle each possible cause one by one.
First: Check for the NUMBA_DISABLE_CUDA Environment Variable
The error message explicitly calls out this environment variable as a possible culprit, so let's start here:
- Open Command Prompt (CMD) and run:
echo %NUMBA_DISABLE_CUDA% - If the output is
1, that's definitely why CUDA is disabled. To fix this:- Right-click "This PC" → Properties → Advanced System Settings → Environment Variables.
- Look for
NUMBA_DISABLE_CUDAin either User Variables or System Variables. - Either delete the variable entirely, or change its value to
0. - Restart your terminal/IDE (this is crucial—changes won't take effect until you do this!) and re-run your code.
Second: Verify Numba Can Detect Your GPU
If the environment variable isn't the issue, let's check if Numba can actually see your GTX 960M:
- Run this simple test script:
from numba import cuda print("Detected GPUs:", cuda.gpus) - If the output is
Detected GPUs: [], that means Numba isn't finding your GPU. Here's what to do next:- Install/Reinstall CUDA Toolkit: Your GTX 960M supports CUDA Compute Capability 5.0, so stick with a compatible CUDA version (CUDA 11.x is a safe bet—avoid the very latest 12.x releases, as they drop support for older GPUs).
- Update System PATH: Make sure the CUDA
bindirectory is added to your system PATH. For example, if you installed CUDA 11.8, addC:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8\binto your PATH. - Restart Your Machine: After installing CUDA or updating PATH, a full restart ensures all changes are applied.
Third: Ensure Version Compatibility
Sometimes mismatched versions between Numba, CUDA, and NumPy can cause issues:
- Update Numba to the latest stable version:
pip install numba --upgrade - Update NumPy as well (outdated NumPy can conflict with Numba's CUDA support):
pip install numpy --upgrade - Double-check that your Numba version is compatible with your installed CUDA Toolkit (focus on major version matches, e.g., Numba 0.57+ works with CUDA 11.x).
Fourth: Tweak Your Test Code (Just to Be Sure)
Your original code looks fine, but adding a quick CUDA availability check can help diagnose issues earlier. Here's an adjusted version:
import numpy as np from numba import vectorize, cuda @vectorize(["float32(float32, float32)"], target='cuda') def test(a, b): return a + b def main(): # Check if CUDA is available first if not cuda.is_available(): print("Error: CUDA is not detected on your system!") return a = np.arange(1, 10, dtype=np.float32) b = np.arange(11, 20, dtype=np.float32) c = test(a, b) print("Result:", c) if __name__ == "__main__": main()
Start with the environment variable check—it's the most likely fix here. If that doesn't work, move through the other steps one by one. You've already put in two weeks of troubleshooting, so let's get this sorted!
内容的提问来源于stack exchange,提问作者Gabeeeh

