Pytorch module error in Jupyter Notebook

13,172

Solution 1

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.

Solution 2

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
Share:
13,172
Admin
Author by

Admin

Updated on June 11, 2022

Comments

  • Admin
    Admin almost 2 years

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

  • xilpex
    xilpex about 5 years
    Could you explain your answer a bit more?
  • Fan
    Fan about 5 years
    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.html
  • mak
    mak about 3 years
    For me the minimal command that worked on my virtual environment was conda install jupyter notebook. Thanks for the tip, it helped.