PyTorch keep installing wrong version

I am having a hard time installing the correct PyTorch version 1.13.
I am using Python 3.9.13,
Cuda compilation tools, release 11.7, V11.7.64,
and pip 22.3.1

following the command from pytorch website:

pip3 install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu117

It keeps installing torch 1.11.0 with no CUDA support.

>>> import torch
>>>
>>> torch.cuda.is_available()
False
>>> torch.__version__
'1.11.0+cpu'
>>> torch.cuda.init()
AssertionError: Torch not compiled with CUDA enabled

How do I fix this?

The command works for me:

pip3 install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu117
Looking in indexes: https://pypi.org/simple, https://download.pytorch.org/whl/cu117
Collecting torch
  Downloading https://download.pytorch.org/whl/cu117/torch-1.13.1%2Bcu117-cp39-cp39-linux_x86_64.whl (1801.8 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.8/1.8 GB 55.0 MB/s eta 0:00:00
Collecting torchvision
  Downloading https://download.pytorch.org/whl/cu117/torchvision-0.14.1%2Bcu117-cp39-cp39-linux_x86_64.whl (24.3 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 24.3/24.3 MB 57.3 MB/s eta 0:00:00
Collecting torchaudio
  Downloading https://download.pytorch.org/whl/cu117/torchaudio-0.13.1%2Bcu117-cp39-cp39-linux_x86_64.whl (4.2 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 4.2/4.2 MB 56.0 MB/s eta 0:00:00
...
Successfully installed certifi-2022.12.7 charset-normalizer-2.1.1 idna-3.4 numpy-1.24.1 pillow-9.4.0 requests-2.28.1 torch-1.13.1+cu117 torchaudio-0.13.1+cu117 torchvision-0.14.1+cu117 typing-extensions-4.4.0 urllib3-1.26.13
Python 3.9.15 | packaged by conda-forge | (main, Nov 22 2022, 15:55:03) 
[GCC 10.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> torch.__version__
'1.13.1+cu117'
>>> import torchvision
>>> torchvision.__version__
'0.14.1+cu117'
>>> import torchaudio
>>> torchaudio.__version__
'0.13.1+cu117'

However, I don’t understand why you don’t use the supported command from the install instructions:

pip3 install torch torchvision torchaudio

instead but want to specify the extra-index-url.

the command is from Start Locally | PyTorch.
I tried to uninstall and install using the command you give

C:\...>pip3 install torch torchvision torchaudio
Collecting torch
  Downloading torch-1.13.1-cp39-cp39-win_amd64.whl (162.5 MB)
     ---------------------------------------- 162.5/162.5 MB 3.1 MB/s eta 0:00:00
Requirement already satisfied: torchvision in c:\...\python\python39\lib\site-packages (0.12.0)
Requirement already satisfied: torchaudio in c:\...\python\python39\lib\site-packages (0.11.0)
Requirement already satisfied: typing-extensions in c:\users\...\python\python39\lib\site-packages (from torch) (4.4.0)
  Using cached torch-1.11.0-cp39-cp39-win_amd64.whl (157.9 MB)
Requirement already satisfied: numpy in c:\users\...\python\python39\lib\site-packages (from torchvision) (1.24.1)
Requirement already satisfied: pillow!=8.3.*,>=5.3.0 in c:\users\...\python\python39\lib\site-packages (from torchvision) (9.4.0)
Requirement already satisfied: requests in c:\users\...\python\python39\lib\site-packages (from torchvision) (2.28.1)
Requirement already satisfied: certifi>=2017.4.17 in c:\users\...\python\python39\lib\site-packages (from requests->torchvision) (2022.12.7)
Requirement already satisfied: charset-normalizer<3,>=2 in c:\users\...\python\python39\lib\site-packages (from requests->torchvision) (2.1.1)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in c:\users\...\python\python39\lib\site-packages (from requests->torchvision) (1.26.13)
Requirement already satisfied: idna<4,>=2.5 in c:\users\...\python\python39\lib\site-packages (from requests->torchvision) (3.4)
Installing collected packages: torch
Successfully installed torch-1.11.0

it downloaded the right version then installed 1.11.0 instead

Sorry, I didn’t realize you are using Windows in which case your command seems to be right.

Using cached torch-1.11.0-cp39-cp39-win_amd64.whl (157.9 MB)

this indicates that a cached version is used for whatever reason. Uninstall all PyTorch binaries from the current environment and reinstall it via pip install --no-cache-dir ...

Same thing, maybe the python version? Could the python does not recognize pytorch 1.13?

C:\...>pip3 install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu117 --no-cache-dir
Looking in indexes: https://pypi.org/simple, https://download.pytorch.org/whl/cu117
Collecting torch
  Downloading https://download.pytorch.org/whl/cu117/torch-1.13.1%2Bcu117-cp39-cp39-win_amd64.whl (2255.6 MB)
     ---------------------------------------- 2.3/2.3 GB 3.5 MB/s eta 0:00:00
Requirement already satisfied: torchvision in c:\users\...\appdata\local\programs\python\python39\lib\site-packages (0.12.0)
Requirement already satisfied: torchaudio in c:\users\...\appdata\local\programs\python\python39\lib\site-packages (0.11.0)
Requirement already satisfied: typing-extensions in c:\users\...\appdata\local\programs\python\python39\lib\site-packages (from torch) (4.4.0)
Requirement already satisfied: requests in c:\users\...\appdata\local\programs\python\python39\lib\site-packages (from torchvision) (2.28.1)
  Downloading torch-1.11.0-cp39-cp39-win_amd64.whl (157.9 MB)
     ---------------------------------------- 157.9/157.9 MB 3.5 MB/s eta 0:00:00
Requirement already satisfied: pillow!=8.3.*,>=5.3.0 in c:\users\...\appdata\local\programs\python\python39\lib\site-packages (from torchvision) (9.4.0)
Requirement already satisfied: numpy in c:\users\...\appdata\local\programs\python\python39\lib\site-packages (from torchvision) (1.24.1)
Requirement already satisfied: idna<4,>=2.5 in c:\users\...\appdata\local\programs\python\python39\lib\site-packages (from requests->torchvision) (3.4)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in c:\users\...\appdata\local\programs\python\python39\lib\site-packages (from requests->torchvision) (1.26.13)
Requirement already satisfied: certifi>=2017.4.17 in c:\users\...\appdata\local\programs\python\python39\lib\site-packages (from requests->torchvision) (2022.12.7)
Requirement already satisfied: charset-normalizer<3,>=2 in c:\users\...\appdata\local\programs\python\python39\lib\site-packages (from requests->torchvision) (2.1.1)
Installing collected packages: torch
Successfully installed torch-1.11.0

Your torchvision and torchaudio versions are too old (0.12.0 and 0.11.0, respectively, while 0.14.1 and 0.13.1 would be needed as seen in my output).
I guess pip finds these packages and downgrades PyTorch again, so uninstall all libs depending on PyTorch and rerun the install command again.
Alternatively, create a new, clean, and empty virtual environment and install the binaries there.