Notebook: No module named 'torch'

Created on 14 May 2019  路  6Comments  路  Source: jupyter/notebook

I have installed pytorch in virtual environment. When I am trying to execute import torch from Jupyter notebook I am getting error as below.

ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-10-eb42ca6e4af3> in <module>
----> 1 import torch
ModuleNotFoundError: No module named 'torch'

But when I am running the same in Using Anaconda command prompt, import torch is successful. I am running Jupyter in windows 64 Anaconda environment

Most helpful comment

I had the same issue. 'import torch' worked on the terminal but it didn't work on the Jupyter.
First, on the Jupyter, try to type and see the output of:

print(sys.executable)

I found that the location of the executable was from a different conda environment.
What finally worked for me was:

  1. On the top of the Jupyter window, click the "Kernel" drop-down menu.
  2. Move the mouse over "Change kernel". Then, I could see a list of different Python conda environments. I noticed that the one that was selected was for Tensorflow. I changed the kernel to
    > Python [conda env: XXX]
    where XXX is whatever the name of the conda environment where Pytorch was set up.

At least it worked for me after hours of frustrating moment. Hope it helps.

All 6 comments

I have the same issue.
I've seen this (issues/4827), which is related to the missing lib mkl. But, in my case, I have mkl-2019.4 and it still doesn't work.
What is awkward is that this import works fine in spyder, the problem is only in jupyter notebook.

It turns out that the version mkl-2019.4 doesn't really work. I have installed the 2018 and it's fine now.
conda install mkl=2018

I had the same issue. 'import torch' worked on the terminal but it didn't work on the Jupyter.
First, on the Jupyter, try to type and see the output of:

print(sys.executable)

I found that the location of the executable was from a different conda environment.
What finally worked for me was:

  1. On the top of the Jupyter window, click the "Kernel" drop-down menu.
  2. Move the mouse over "Change kernel". Then, I could see a list of different Python conda environments. I noticed that the one that was selected was for Tensorflow. I changed the kernel to
    > Python [conda env: XXX]
    where XXX is whatever the name of the conda environment where Pytorch was set up.

At least it worked for me after hours of frustrating moment. Hope it helps.

if nothing work with you try to install Jupiter notebook from anaconda navigator
it's the only way it works with me

https://janakiev.com/blog/jupyter-virtual-envs/

This fixed my issue. If it helps anyone

I solved this issue by installing Jupyter notebook in current environment.

Was this page helpful?
0 / 5 - 0 ratings