ModuleNotFoundError: No module named 'torch' solution

Encountered the following problem when running the python program: ModuleNotFoundError: No module named 'torch'
insert image description here
This is caused by not installing the torch package. We can easily install it in pycharm or on the cmd command line, but because the torch installation package has passed Large, for example, torch-1.9.1 is about 200M. The above two methods are downloaded directly from GitHub. Without suitable tools, it will take a long time, even an hour or two.
insert image description here

This article mainly describes a method of installing a large software package in pycharm by downloading torch's whl file from the Tsinghua mirror official website for installation. (Torchvision is also applicable)
1. Download torch from Tsinghua mirror official website
Tsinghua mirror torch download
Tsinghua mirror torchvision
download pay attention to the computer system and python version, for example, my computer is Windows10, 64-bit, installed python3.8, want to install 1.8. 1 version of torch, you can download the whl file as shown in the figure
insert image description here

If you want to install torchvision version 0.9.1, you can download the whl file as shown in the figure.
insert image description here
Note: torch1.8.1 and torchvision0.9.1 can work together. If the version does not match, the following error may occur:
insert image description here

The downloaded file is shown in the figure, it is recommended to place it directly in the D drive
insert image description here

2. Press Win+R, enter cmd, and enter the command line
insert image description here
3. Enter D: and press Enter to enter the D drive (note the English semicolon),
which is to enter the path where the torch file was just saved
insert image description here
4. Enter pip install torch- 1.8.1-cp38-cp38-win_amd64.whl and press Enter
(torch-1.9.1-cp38-cp38-win_amd64.whl is the file name plus suffix)
insert image description here

Shows successful installation of torch

5. Enter pip install torchvision-0.9.1-cp38-cp38-win_amd64.whl and press Enter
(torchvision-0.9.1-cp38-cp38-win_amd64.whl is the file name plus suffix)
insert image description here
to show successful installation of torchvision

Pay special attention to the versions of torch and torchvision. If the versions do not match, errors may occur. Torch1.8.1 and torchvision0.9.1 are a matching version

At this time, if you run the program again, there will be no ModuleNotFoundError: No module named 'torch' error.

Guess you like

Origin blog.csdn.net/thy0000/article/details/122652349