[Solution] The system has installed pytorch but cannot call it, and the error ModuleNotFoundError: No module named 'torch' is reported.

[Solution] The system has installed pytorch but cannot call it, and the error ModuleNotFoundError: No module named 'torch' is reported.

introduction

When configuring the cloud server, it shows that there is already a pytorch environment, but when running, an error is reported saying that there is no such environment. This is caused by not entering the environment configured by pytorch. You can run pytorch by entering the corresponding environment.

solution

First, the error is as follows:
Insert image description here
Solution:
Ctrl+Z to exit python, enter the following command to confirm that the system has pytorch

conda env list

As shown in the figure below, you can see that the name of the environment where pytorch is located is Pytorch-gpu.
Insert image description here
Then enter the following command to enter the environment.

conda activate Pytorch-gpu

The appearance of (Pytorch-gpu) on the left indicates that you are currently in the Pytorch-gpu environment. There is no problem in importing torch.
Insert image description here

pytorch installation

1.Pytorch installation under cpu

pip install torch

2. Pytorch installation under GPU.
First enter the pytorch official website
. Scroll down and enter your own version corresponding to each environment. The bottom line is the installation command. Open the command line and enter the command. Note: You can also create
Insert image description here
a separate environment through the following command. Enter the environment to install torch to achieve the effects of multiple environments mentioned above.
Create an environment, where the pytorch1 environment name can be customized.

conda create -n pytorch1 python=3.9

Enter the environment

conda activate pytorch1

Guess you like

Origin blog.csdn.net/qq_43605229/article/details/124809610