DEV Community

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

Posted on

5 1

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

Heroku

Built for developers, by developers.

Whether you're building a simple prototype or a business-critical product, Heroku's fully-managed platform gives you the simplest path to delivering apps quickly — using the tools and languages you already love!

Learn More

Top comments (0)

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay