Pytorch cannot detect cuda in script

I am facing a situation when I try torch.cuda.is_available() on the terminal, the result is True but then when I run the same in the script the result is False, leading to the error No cuda devices is available. Have anyone faced the same situation before?

Could you describe what “in the terminal” and “in the script” exactly means? Typically these issues are caused by different virtual environments being selected e.g. in Jupyter vs. your terminal so something similar could also be the case here.

Sorry for the unclear context. In the script means the python code written on file could not detect cuda device, however, if I try the command python -c "import torch; print(torch.cuda.is_available())" then I got the True return as you can see on this screenshot

I have tried to reinstall my environment with python 3.9 and everything seems OK now. The error mentioned above is with python 3.11.