Unable to import Pytorch module in Jupyter Notebook
Asked Answered
S

6

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

Soursop answered 18/1, 2018 at 8:22 Comment(2)
Possible duplicate of this questionRhu
same problem: import succeeds from python but fails from ipython/jupyterPolash
P
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.

Petey answered 18/11, 2018 at 3:17 Comment(1)
For me the minimal command that worked on my virtual environment was conda install jupyter notebook. Thanks for the tip, it helped.Aylmar
M
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
Mechelle answered 8/7, 2019 at 4:7 Comment(0)
H
1
  1. Go to anaconda.navigator -> environments -> base(root) -> select not installed -> search 'torch' -> install 2.run 'import torch' in your jupyter notebook!
Haddock answered 3/3, 2021 at 15:13 Comment(0)
C
1

Use below command-

conda install pytorch torchvision torchaudio cudatoolkit=10.2 -c pytorch
Cimmerian answered 6/11, 2021 at 15:38 Comment(0)
B
0

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

Baghdad answered 8/4, 2019 at 18:53 Comment(2)
Could you explain your answer a bit more?Ivie
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 ipython.readthedocs.io/en/stable/install/kernel_install.htmlBaghdad
W
0

I had the same issue while I was running the notebook instance in a python virtual environment.

$ python3 -m venv myenv
$ source myenv/bin/activate
$ jupyter notebook

After this, I ran !pip3 install torch. While it got installed successfully, I was getting the error No module named 'torch'. Restarting the kernel did not work for me so I ran the following command and restarted the Jupyter notebook instance.

$ pip3 install ipykernel
$ jupyter notebook

Strangely, I was facing some issue trying to start the notebook instance. However, reinstalling notebook using pip3 install jupyter worked for me and I could import the library successfully.

Wodge answered 7/7 at 17:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.