6

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...

malioboro
  • 3,097
  • 4
  • 35
  • 55
Chaeun Lee
  • 61
  • 1
  • 1
  • 3
  • 1
    Possible duplicate of [this question](https://stackoverflow.com/questions/39604271/conda-environments-not-showing-up-in-jupyter-notebook?rq=1) – nnnmmm Jan 18 '18 at 10:47
  • same problem: import succeeds from python but fails from ipython/jupyter – Jean-Pat Oct 17 '18 at 11:02

5 Answers5

11

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.

MLDev
  • 326
  • 4
  • 8
  • For me the minimal command that worked on my virtual environment was `conda install jupyter notebook`. Thanks for the tip, it helped. – mak Mar 29 '21 at 04:55
1

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
Caíque de Paula
  • 424
  • 5
  • 17
1
  1. Go to anaconda.navigator -> environments -> base(root) -> select not installed -> search 'torch' -> install 2.run 'import torch' in your jupyter notebook!
vjc
  • 11
  • 3
0

You need to create a kernel in the virtual envs and choose that kernel in the jupyter.

Fan
  • 1
  • Could you explain your answer a bit more? – xilpex Apr 08 '19 at 19:17
  • If you want to have multiple IPython kernels for different virtual envs or condo environments, you will need to specify unique names for the kernelspecs. You can check the command in https://ipython.readthedocs.io/en/stable/install/kernel_install.html – Fan Apr 11 '19 at 06:41
0

Use below command-

conda install pytorch torchvision torchaudio cudatoolkit=10.2 -c pytorch
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Anj
  • 99
  • 5