No module named “Torch”

Total
0
Shares

I successfully installed pytorch via conda:

conda install pytorch-cpu torchvision-cpu -c pytorch

I also successfully installed pytorch via pip:

pip3 install https://download.pytorch.org/whl/cpu/torch-1.0.1-cp36-cp36m-win_amd64.whl
pip3 install torchvision

But, it only works in a jupyter notebook. Whenever I try to execute a script from the console, I get the error message:

No module named "torch"


Solution

Try to install PyTorch using pip:

First create a Conda environment using:

conda create -n env_pytorch python=3.6

Activate the environment using:

conda activate env_pytorch

Now install PyTorch using pip:

pip install torchvision 

Note: This will install both torch and torchvision.

Now go to Python shell and import using the command:

import torch
import torchvision

Source: StackOverflow.com

Leave a Reply

Your email address will not be published. Required fields are marked *