notebook icon indicating copy to clipboard operation
notebook copied to clipboard

No module named 'torch'

Open jonanem opened this issue 6 years ago • 11 comments
trafficstars

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

jonanem avatar May 14 '19 07:05 jonanem

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.

markfsousa avatar Aug 04 '19 17:08 markfsousa

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

markfsousa avatar Aug 04 '19 18:08 markfsousa

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.

dps42 avatar Nov 07 '19 20:11 dps42

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

saraXX avatar Jun 13 '20 00:06 saraXX

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

This fixed my issue. If it helps anyone

Raunaque97 avatar Nov 20 '20 19:11 Raunaque97

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

hj0921 avatar Dec 28 '20 03:12 hj0921

I had the same issue reported by dps42:

'import torch' worked on the terminal but it didn't work on the Jupyter.

for me, calling conda install jupyter notebook worked as mentioned in stackoverflow: Pytorch module error in Jupyter Notebook

makwadajp avatar Mar 29 '21 05:03 makwadajp

I had similar problem: torch being imported in terminal but not in same environments jupyter notebook.

I first got, print(sys.executable), in root directory.

To solve:

I install jupyter notebook in the virutal environment. Later torch was being imported into jupyter notebook. The print(sys.executable), now gave the location of file in conda virtual environment.

Thanks all !!!

malodhi avatar Apr 13 '21 12:04 malodhi

When i using conda install jupyter notebook I got 500 : Internal Server Error when opening my jupyter notebook

jonathonyan avatar Apr 16 '21 21:04 jonathonyan

Don't forget to restart your IDE and server after you installed jupyter and notebook.

AkagawaTsurunaki avatar Dec 06 '23 13:12 AkagawaTsurunaki

conda install jupyter notebook fixed my issue. I believe the jupyter was pointing to the one downloaded from pip. Installing it from conda should override this

jazelly avatar Apr 23 '24 13:04 jazelly