The pycharm console torch.cuda.is-available () is true, but it is false in the script code!

username: yingmengxi3
cooked: <p>Platform:Windows<br>
compiler:Pycharm<br>
Environment:</p>
<ol>
<li>[Python]= 3.9</li>
<li>[Cuda]= 11.3</li>
<li>[Pytorch]= 1.12</li>
</ol>
<p>In cmd:</p>
<pre><code class="lang-auto">(python_39) D:\PythonFolder\prophet-main\prophet-main&gt;python
Python 3.9.19 (main, May  6 2024, 20:12:36) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
&gt;&gt;&gt; import torch
&gt;&gt;&gt; torch.cuda.is_available()
True
</code></pre>
<p>But return false(“torch.cuda.is not available”) in the code:</p>
<pre><code class="lang-auto">if torch.cuda.is_available():
                print("torch.cuda.is_available")
            else:
                print("torch.cuda.is not available")
</code></pre>
<p>Does anyone know how to solve this problem? Thanks a lot!</p>

username: yingmengxi3
cooked: <p>conda list:</p>
<pre><code class="lang-auto">(python_39) D:\PythonFolder\prophet-main\prophet-main&gt;conda list
# packages in environment at D:\PythonFolder\Anaconda\envs\python_39:
#
# Name                    Version                   Build  Channel
args                      0.1.0                    pypi_0    pypi
blas                      1.0                         mkl    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
brotli-python             1.0.9            py39hd77b12b_8    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
ca-certificates           2024.7.2             haa95532_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
certifi                   2024.8.30        py39haa95532_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
charset-normalizer        3.3.2              pyhd3eb1b0_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
click                     8.1.7                    pypi_0    pypi
clint                     0.5.1                    pypi_0    pypi
clip                      1.0                      pypi_0    pypi
colorama                  0.4.6                    pypi_0    pypi
commonmark                0.9.1                    pypi_0    pypi
coverage                  7.6.1                    pypi_0    pypi
cudatoolkit               11.3.1               h59b6b97_2    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
einops                    0.6.0                    pypi_0    pypi
et-xmlfile                1.1.0                    pypi_0    pypi
filelock                  3.16.0                   pypi_0    pypi
freetype                  2.12.1               ha860e81_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
fsspec                    2024.9.0                 pypi_0    pypi
ftfy                      6.2.3                    pypi_0    pypi
huggingface-hub           0.24.6                   pypi_0    pypi
idna                      3.7              py39haa95532_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
intel-openmp              2023.1.0         h59b6b97_46320    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
joblib                    1.4.2                    pypi_0    pypi
jpeg                      9e                   h827c3e9_3    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
lcms2                     2.12                 h83e58a3_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
lerc                      3.0                  hd77b12b_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
libdeflate                1.17                 h2bbff1b_1    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
libpng                    1.6.39               h8cc25b3_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
libtiff                   4.5.1                hd77b12b_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
libuv                     1.48.0               h827c3e9_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
libwebp-base              1.3.2                h2bbff1b_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
lz4-c                     1.9.4                h2bbff1b_1    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
mamba                     0.11.3                   pypi_0    pypi
mkl                       2023.1.0         h6b88ed4_46358    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
mkl-service               2.4.0            py39h2bbff1b_1    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
mkl_fft                   1.3.10           py39h827c3e9_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
mkl_random                1.2.7            py39hc64d2fc_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
nltk                      3.9.1                    pypi_0    pypi
numpy                     1.21.2                   pypi_0    pypi
openai                    0.18.0                   pypi_0    pypi
opencv-python             4.5.5.64                 pypi_0    pypi
openjpeg                  2.5.2                hae555c5_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
openpyxl                  3.1.5                    pypi_0    pypi
openssl                   3.0.15               h827c3e9_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
opt-einsum                3.3.0                    pypi_0    pypi
packaging                 24.1                     pypi_0    pypi
pandas                    2.2.2                    pypi_0    pypi
pandas-stubs              2.2.2.240807             pypi_0    pypi
pillow                    9.3.0                    pypi_0    pypi
pip                       24.2             py39haa95532_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
pygments                  2.18.0                   pypi_0    pypi
pysocks                   1.7.1            py39haa95532_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
python                    3.9.19               h1aa4202_1    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
python-dateutil           2.9.0.post0              pypi_0    pypi
pytorch                   1.12.0          py3.9_cuda11.3_cudnn8_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch
pytorch-mutex             1.0                        cuda    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch
pytz                      2024.2                   pypi_0    pypi
pyyaml                    6.0                      pypi_0    pypi
vs2015_runtime            14.40.33807          h98bb1dd_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
wcwidth                   0.2.13                   pypi_0    pypi
wheel                     0.43.0           py39haa95532_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
win_inet_pton             1.1.0            py39haa95532_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
xz                        5.4.6                h8cc25b3_1    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
yaml                      0.2.5                he774522_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
zlib                      1.2.13               h8cc25b3_1    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
zstd                      1.5.5                hd43e919_2    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main

</code></pre>
<p>nvidia-smi:</p>
<pre><code class="lang-auto">(python_39) D:\PythonFolder\prophet-main\prophet-main&gt;nvidia-smi
Thu Sep 19 21:38:43 2024                                                                 
+---------------------------------------------------------------------------------------+
| NVIDIA-SMI 546.17                 Driver Version: 546.17       CUDA Version: 12.3     |
|-----------------------------------------+----------------------+----------------------+
| GPU  Name                     TCC/WDDM  | Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp   Perf          Pwr:Usage/Cap |         Memory-Usage | GPU-Util  Compute M. |
|                                         |                      |               MIG M. |
|=========================================+======================+======================|
|   0  NVIDIA GeForce RTX 4060      WDDM  | 00000000:01:00.0  On |                  N/A |
|  0%   47C    P8              N/A / 115W |    997MiB /  8188MiB |      2%      Default |
|                                         |                      |                  N/A |
+-----------------------------------------+----------------------+----------------------+
                                                                                  
</code></pre>
<p>nvcc -V:</p>
<pre><code class="lang-auto">(python_39) D:\PythonFolder\prophet-main\prophet-main&gt;nvcc -V
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2021 NVIDIA Corporation
Built on Mon_May__3_19:41:42_Pacific_Daylight_Time_2021
Cuda compilation tools, release 11.3, V11.3.109
Build cuda_11.3.r11.3/compiler.29920130_0

</code></pre>

username: ptrblck
cooked: <p>You are most likely not using the same virtual environment in PyCharm and your terminal and you could check the installation location of PyTorch e.g. by printing its <code>torch.__path__</code>.</p>