Yes it is possible to set it within python but it has to be visible and be done before the cuda init.
The thing is that it can be tricky for a user to know when the cuda init is going to happen so for my scripts, I assume that it does not work and set it outside, that way I’m sure it always works, even if an obscure dependency changed and now initialize cuda before I set the variable.