Modulenotfounderror: No module named ‘torch’ (Fix the error)

Modulenotfounderror_ No module named ‘torch’

Modulenotfounderror: No module named ‘torch’ error occurs when PyTorch python module is either not found or its path is not set. Actually, Pytorch is a deep learning library that is compatible with multiple hardware configurations like CPU and CUDA-supported GPUs. The installation process of Pytorch is slightly different for different hardware configurations. Actually, the commands are a bit different. Anyways in this article, we will explore multiple ways to fix this  (No module named ‘torch’) error. So let’s see.

Modulenotfounderror: No module named ‘torch’ ( Solution ) –

We can use any python package manager for the installation of the PyTorch module. Here we will explore pip and conda as package managers or we can use source code to install pytorch package.

Pytorch
Pytorch

Solution 1: Using pip to install pytorch –

Actually, the package manager installs the binary /wheel file at the required path to execute in a single command. Here is the pip command to install PyTorch.

pip install torch

The above command will install the latest version of the PyTorch module. You can specify any other version with the same command.

Modulenotfounderror No module named torch
Modulenotfounderror No module named torch
pip install torch==version

Now if you are using python 3 then specifically we can use pip3 in the above command.

Other Supplementary packages –

Mostly we do not use any PyTorch alone. Actually, there are a couple of packages we use as a bundle. Here is the command for installing them all together.

pip install torch torchvision torchaudio

Hardware specification with pip –

Now let’s see the hardware configurations with pytorch module. Mostly Deep Learning application works smoothly with  CUDA GPU supportive library. Here is the command for that.

pip3 install torch==1.10.0+cu113

If you want to install CUDA supportive supplementary packages. Use the below command.

pip3 install torch==1.10.0+cu113 torchvision==0.11.1+cu113 torchaudio===0.10.0+cu113 -f

Solution 2: Using conda to install Pytorch –

Conda package manager comes default with Anaconda distribution. Just quite similar to pip we can use the below command.

conda install -c pytorch pytorch
conda installation with Pytorch
conda installation with Pytorch

 

Installing Other Supplementary packages with Conda –

Just like the above package manager pip, we can install additional packages with conda too. Follow the below command for that.

conda install pytorch torchvision torchaudio cpuonly -c pytorch

Hardware specification with conda –

To install the CUDA supportive libraries, Here is the command.

conda install pytorch torchvision torchaudio cudatoolkit=11.3 -c pytorch

Solution 3: Pytorch Installation using source code-

We can download the source code from GitHub and using the setup.py file we can install the same. This is not very straight but one of the ways to solve module not found error.

Download the source code for pytorch.

pytorch installation using source code
pytorch installation using source code
python setup.py install

 

Thanks

Data Science Learner Team

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner