CUDA_VISIBLE_DEVICES not effective on windows?

I believe this problem is Windows specific, as I have run the same test on Ubuntu 18.04 without problem.


Even after os.environ['CUDA_VISIBLE_DEVICES'] = '', torch still uses CUDA GPU.

I expect RuntimeError: No CUDA GPUs are available with the following command:

python -c "import os; os.environ['CUDA_VISIBLE_DEVICES']=''; import torch; torch.randn(5).cuda()"

However, the command exists without error.

On the other hand, setting CUDA_VISIBLE_DEVICES in cmd works properly:

set CUDA_VISIBLE_DEVICES="" & python -c "import torch; torch.randn(5).cuda()"
-> RuntimeError: No CUDA GPUs are available

Running environment

Windows 10
Python 3.9.7 (default, Sep 16 2021, 16:59:28) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
PyTorch 1.10.2
CUDA 11.3

What I have tried:

The problem occurred to me when I tried pytest’s monkeypatch.setenv.
I tried os.putenv directly which seems to be used internally by os.environ, still unsuccessful.
Then I followed through Python on Windows documentation without solution.
I found no help on PyTorch Windows FAQ either.