Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No module named "Torch"

I installed pytorch via

conda install pytorch-cpu torchvision-cpu -c pytorch

And I also tried

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

Both installed successfully!

But, it only works in jupiter notebook. Whenever I try to execute a script from the console, I get the error message: No module named "torch"

How can I fix this?

like image 983
RedCrayon Avatar asked Feb 23 '19 15:02

RedCrayon


People also ask

How do I install Python torch?

To install PyTorch, you have to run the installation command of PyTorch on your command prompt. This command is available on https://pytorch.org/. Select language and cuda version as per your requirement. Now, run python -version, and Conda -version command to check Conda and python packages are installed or not.

How do I install PyTorch on Anaconda Navigator?

To install Anaconda, you will use the 64-bit graphical installer for PyTorch 3. x. Click on the installer link and select Run . Anaconda will download and the installer prompt will be presented to you.


2 Answers

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 
like image 173
srilekha palepu - Intel Avatar answered Oct 14 '22 22:10

srilekha palepu - Intel


I installed on my macos by the official command:

conda install pytorch torchvision -c pytorch 

but when I follow the official verification I get the same problem like yours.

Then I create a conda virtual environment:

conda create --name learnpytorch python=3.5 

and install pytorch inside the environment:

conda install pytorch torchvision -c pytorch 

run the verification, it works.

Hope these could help you.

like image 24
Wigcat Lion Avatar answered Oct 14 '22 22:10

Wigcat Lion