PyTorch / Jupyter notebook: ModuleNotFoundError: No module named ‘torch’
In this post, you will learn about installing PyTorch, a deep learning library in Jupyter.
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. In this post, I
What is PyTorch ?
PyTorch is an open source machine learning / deep learning framework that is fully featured for building ML / DL models.
Installing PyTorch
As a typical learner, I started with the below command to install `PyTorch` in Jupyter.
!pip install torch

The error
In the next cell, when I run
import torch
torch.__version__
I see an error
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
You may have to restart your kernel after running this command
Now, when I re-run the `PyTorch` command

The key difference between !
and %
is that %
is specific to the Jupyter environment while !
runs in a shell. So, %
the magic command lasts longer in a Jupyter virtual environment
If you like the content, clap 👏 and for more such content in the future
Follow
me. Happy learning :)