DEV Community

Cover image for PyTorch / Jupyter notebook: ModuleNotFoundError: No module named 'torch'
Vidyasagar SC Machupalli
Vidyasagar SC Machupalli

Posted on

PyTorch / Jupyter notebook: ModuleNotFoundError: No module named 'torch'

This post is part of the #shortposts series where I publish my learnings in short ;)

Learning is a part and parcel of my journey. I started revisting Linear Algebra to explore the underpinnings of Artificial Intelligence and Quantum Computing. Linear Algebra is a good to have for AI and Quantum.

Recently, I picked PyTorch along with NumPy, SciPy, MatplotLib, and other Python libraries. Jupyter notebook is my go-to tool to learn AI, Data Science and other Python related topics.

Installing PyTorch

As a typical learner, I started with the below command to install PyTorch in Jupyter.



!pip install torch


Enter fullscreen mode Exit fullscreen mode

Install PyTorch in Jupyter notebook

The error

In the next cell, when I run



import torch
torch.__version__


Enter fullscreen mode Exit fullscreen mode

I see an error
ModuleNotFoundError: No module named 'torch'

ModuleNotFoundError: No module named 'torch'

The fix

After some research, I found that this is a common error and I was asked to run the pip and python commands to check the versions and upgrade if required etc., but nothing worked. Until I bumped into a magic command



%pip install torch


Enter fullscreen mode Exit fullscreen mode

You may have to restart your kernel after running this command

Now, when I re-run the PyTorch command

PyTorch up and running

Top comments (0)