You can use CUDA_VISIBLE_DEVICES to hide all cuda devices but that needs to be done before any cuda initialization (so to be safe I usually do it in the command line like CUDA_VISIBLE_DEVICES=2 python my_prog.py).
The thing is that you won’t be able to change that value dynamically during the program. Only the value at the moment of the cuda initialization will be read and used. This is how cuda works unfortunately and out of our reach.