Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pytorch module error in Jupyter Notebook

I installed pytorch using conda command when the virtual env was activated.

But, there are some problems when I import torch modules in Jupyter Notebook.

I checked the sys.path both in the prompt and in Jupyter Notebook.

Well.. in the prompt, the result of sys.path is

['', '/home/usrname/anaconda3/lib/python36.zip',
     '/home/usrname/anaconda3/lib/python3.6',
     '/home/usrname/anaconda3/lib/python3.6/lib-dynload',
     '/home/usrname/anaconda3/lib/python3.6/site-packages']

and there are no errors when I import torch modules.

But, in the jupyter notebook(executed in chrome), the sys.path is

['',
 '/home/usrname/anaconda3/lib/python36.zip',
 '/home/usrname/anaconda3/lib/python3.6',
 '/home/usrname/anaconda3/lib/python3.6/lib-dynload',
 '/home/usrname/anaconda3/lib/python3.6/site-packages',
 '/home/usrname/anaconda3/lib/python3.6/site-packages/IPython/extensions',
 '/home/usrname/.ipython']

and I see an error: No module named 'torch'

I can't solve this problem...

like image 528
Chaeun Lee Avatar asked Jan 18 '18 08:01

Chaeun Lee


People also ask

Why I Cannot import torch in Jupyter notebook?

This is most probably because you are using a CUDA variant of PyTorch on a system that doesn't have GPU driver installed. That is to say, if you don't have a Nvidia GPU card, please install the cpu-only package according to the commands on https://pytorch.org.

Is PyTorch included in Anaconda?

Anaconda is the recommended package manager as it will provide you all of the PyTorch dependencies in one, sandboxed install, including Python.

How do you solve Modulenotfounderror No module named torch?

First of all Just Create conda environment. conda create -n env_pytorch python=3.6 And then Activate Environment. conda activate env_pytorch Now Just install PyTorch using pip This command. This will install both torch and torchvision.


2 Answers

I had the same issues with this but managed to solve the problem. I think PyTorch has an 'issue' with the regular Jupyter application in Anaconda, so I urge you to first install the numpy jupyter notebook that supports PyTorch, then you can launch you notebook again. Use (while in your virtual env path):

conda install numpy jupyter notebook

Hope this helps.

like image 182
MLDev Avatar answered Oct 20 '22 21:10

MLDev


This works for me:

1.Create a conda virtual environment:

conda create -n env_pytorch python=3.6

2.Active this environment create above:

source activate env_pytorch

3.Install PyTorch with pip or pip3:

pip install torchvision --user
like image 1
Caíque Coelho Avatar answered Oct 20 '22 22:10

Caíque Coelho