How to Solve Python ModuleNotFoundError: no module named ‘torch’

by | Programming, Python, Tips

When using Python, a common error you may encounter is modulenotfounderror: no module named ‘torch’. This error occurs when Python cannot detect the PyTorch library in your current environment. This tutorial goes through the exact steps to troubleshoot this error for the Windows, Mac and Linux operating systems.


ModuleNotFoundError: no module named ‘torch’

What is PyTorch?

PyTorch is an open-source deep learning framework developed by Facebook’s AI Research lab. PyTorch provides a beginner-friendly and Pythonic API for building complex models for research and industrial applications.

The simplest way to install PyTorch is to use the package manager for Python called pip. The following installation instructions are for the major Python version 3.

When you want to install PyTorch using pip, the packages to install together are torch, torchvision, and torchaudio.

How to Install PyTorch on Windows Operating System

First, you need to download and install Python on your PC. Ensure you select the install launcher for all users and Add Python to PATH checkboxes. The latter ensures the interpreter is in the execution path. Pip is automatically on Windows for Python versions 2.7.9+ and 3.4+.

You can check your Python version with the following command:

python3 --version

You can install pip on Windows by downloading the installation package, opening the command line and launching the installer. You can install pip via the CMD prompt by running the following command.

python get-pip.py

You may need to run the command prompt as administrator. Check whether the installation has been successful by typing.

pip --version

PyTorch installation with Pip on Windows for CPU

To install PyTorch for CPU, run the following command from the command prompt.

pip3 install torch torchvision torchaudio

PyTorch installation with Pip on Windows for CUDA 10.2

To install PyTorch for CUDA 10.2, run the following command from the command prompt.

pip3 install torch==1.10.0+cu102 torchvision==0.11.1+cu102 torchaudio===0.10.0+cu102 -f https://download.pytorch.org/whl/cu102/torch_stable.html

PyTorch installation with Pip on Windows for CUDA 11.3

To install PyTorch for CUDA 11.3, run the following command from the command prompt.

pip3 install torch==1.10.0+cu113 torchvision==0.11.1+cu113 torchaudio===0.10.0+cu113 -f https://download.pytorch.org/whl/cu113/torch_stable.html

How to Install PyTorch on Mac Operating System

Open a terminal by pressing command (⌘) + Space Bar to open the Spotlight search. Type in terminal and press enter. To get pip, first ensure you have installed Python3:

python3 --version
Python 3.8.8

Download pip by running the following curl command:

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py

The curl command allows you to specify a direct download link. Using the -o option sets the name of the downloaded file.

Install pip by running:

python3 get-pip.py

From the terminal, use pip3 to install PyTorch:





Note that this is the only way to install PyTorch using pip on the Mac operating system because the macOS binaries do not support CUDA. You can install from the source if you need CUDA.

How to Install PyTorch on Linux Operating Systems

All major Linux distributions have Python installed by default. However, you will need to install pip. You can install pip from the terminal, but the installation instructions depend on the Linux distribution you are using. You will need root privileges to install pip. Open a terminal and use the commands relevant to your Linux distribution to install pip.

Installing pip for Ubuntu, Debian, and Linux Mint

sudo apt install python-pip3

Installing pip for CentOS 8 (and newer), Fedora, and Red Hat

sudo dnf install python-pip3

Installing pip for CentOS 6 and 7, and older versions of Red Hat

sudo yum install epel-release

sudo yum install python-pip3

Installing pip for Arch Linux and Manjaro

sudo pacman -S python-pip

Installing pip for OpenSUSE

sudo zypper python3-pip

PyTorch installation on Linux with Pip for CPU

Once you have installed pip, you can install PyTorch using:

pip3 install torch==1.10.0+cpu torchvision==0.11.1+cpu torchaudio==0.10.0+cpu -f https://download.pytorch.org/whl/cpu/torch_stable.html

PyTorch installation on Linux with Pip for CUDA 10.2

Once you have installed pip, you can install PyTorch using:

pip3 install torch torchvision torchaudio

PyTorch installation on Linux with Pip for CUDA 11.3

Once you have installed pip, you can install PyTorch using:

pip3 install torch==1.10.0+cu113 torchvision==0.11.1+cu113 torchaudio==0.10.0+cu113 -f https://download.pytorch.org/whl/cu113/torch_stable.html

Installing PyTorch Using Anaconda

First, to create a conda environment to install PyTorch.

conda create -n pytorch python=3.6 numpy=1.13.3 scipy

Then activate the PyTorch container. You will see “pytorch” in parentheses next to the command line prompt.

source activate pytorch

Now you’re ready to install PyTorch using conda. The command will change based on the operating system and whether or not you need CUDA.

Installing PyTorch Using Anaconda for CPU

Anaconda is a distribution of Python and R for scientific computing and data science. You can install Anaconda by going to the installation instructions. Once you have installed Anaconda and created your conda environment, you can install PyTorch using the following command:

conda install pytorch torchvision torchaudio cpuonly -c pytorch

Note that this is the only way to install PyTorch using conda on the Mac operating system because the macOS binaries do not support CUDA. You can install from the source if you need CUDA.

Installing PyTorch Using Anaconda for CUDA 10.2

Anaconda is a distribution of Python and R for scientific computing and data science. You can install Anaconda by going to the installation instructions. Once you have installed Anaconda, you can install PyTorch using the following command:

conda install pytorch torchvision torchaudio cudatoolkit=10.2 -c pytorch

Installing PyTorch Using Anaconda for CUDA 11.3

Anaconda is a distribution of Python and R for scientific computing and data science. You can install Anaconda by going to the installation instructions. Once you have installed Anaconda, you can install PyTorch using the following command:

conda install pytorch torchvision torchaudio cudatoolkit=11.3 -c pytorch

Check PyTorch Version

Once you have successfully installed PyTorch, you can use two methods to check the version of PyTorch. First, you can use pip show from your terminal. Remember that the name of the PyTorch package is torch.

pip show torch
Name: torch
Version: 1.10.0+cu102
Summary: Tensors and Dynamic neural networks in Python with strong GPU acceleration
Home-page: https://pytorch.org/
Author: PyTorch Team
Author-email: [email protected]
License: BSD-3
Location: /home/ubuntu/anaconda3/envs/tensorflow2_latest_p37/lib/python3.7/site-packages
Requires: typing-extensions
Required-by: torchaudio, torchvision

Second, within your python program, you can import torch and then reference the __version__ attribute:

import torch

print(torch.__version__)
1.10.0+cu102

If you used conda to install PyTorch, you could check the version using the following command:

conda list -f pytorch

Summary

Congratulations on reading to the end of this tutorial. The modulenotfounderror occurs if you misspell the module name, incorrectly point to the module path or do not have the module installed in your Python environment. If you do not have the module installed in your Python environment, you can use pip to install the package. However, you must ensure you have pip installed on your system. You can also install Anaconda on your system and use the conda install command to install PyTorch.

For further reading on operations with PyTorch, go to the article: How to Convert NumPy Array to PyTorch Tensor.

Go to the online courses page on Python to learn more about Python for data science and machine learning.

For further reading on missing modules, go to the articles:

Have fun and happy researching!

Profile Picture
Senior Advisor, Data Science | [email protected] | + posts

Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.

Buy Me a Coffee ✨