pythonpiptorch

Python3 + Digital Ocean: No module named 'torch'


I am trying to install torch on a Digital Ocean Ubuntu droplet. I am following this:

mkdir ~/pytorch
mkdir ~/pytorch/assets
cd ~/pytorch
python3 -m venv pytorch
source pytorch/bin/activate

Running the command pip install torch==1.7.1+cpu torchvision==0.8.2+cpu -f https://download.pytorch.org/whl/torch_stable.html results in the error:

ERROR: Could not find a version that satisfies the requirement torch==1.7.1+cpu (from versions: 1.13.0, 1.13.0+cpu, 1.13.0+cu116, 1.13.0+cu117, 1.13.0+cu117.with.pypi.cudnn, 1.13.1, 1.13.1+cpu, 1.13.1+cu116, 1.13.1+cu117, 1.13.1+cu117.with.pypi.cudnn, 2.0.0, 2.0.0+cpu, 2.0.0+cpu.cxx11.abi, 2.0.0+cu117, 2.0.0+cu117.with.pypi.cudnn, 2.0.0+cu118, 2.0.1, 2.0.1+cpu, 2.0.1+cpu.cxx11.abi, 2.0.1+cu117, 2.0.1+cu117.with.pypi.cudnn, 2.0.1+cu118, 2.0.1+rocm5.3, 2.0.1+rocm5.4.2, 2.1.0, 2.1.0+cpu, 2.1.0+cpu.cxx11.abi, 2.1.0+cu118, 2.1.0+cu121, 2.1.0+cu121.with.pypi.cudnn, 2.1.0+rocm5.5, 2.1.0+rocm5.6)
ERROR: No matching distribution found for torch==1.7.1+cpu

So I have tried both the following instead (which seem to run successfully):

pip3 install torch torchvision
pip install torch torchvision

However when I try importing torch in python, I get an error:

>>> import torch
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'torch'

Why can't python find torch? What do I need to do to ensure I can use the module?


Solution

  • Use the following command to install PyTorch for CPU in Linux (from the 'get started' docs)

    source pytorch/bin/activate
    python -m pip install torch torchvision --index-url https://download.pytorch.org/whl/cpu
    python -c 'import torch'  # check that torch is found
    

    It is possible that pip is pointing to a different python distribution, which might explain why torch is not found by python.

    Note the use of python -m pip instead of pip. That uses the pip that is associated with python. Using a naked pip might point to a differnet python installation.