[SOLVED] Use pip and conda commands to install pytorch packages under Windows

Use pip and conda commands to install pytorch packages under Windows

Since writing code requires the use of the torch package, but using Pycharm's File -> Setting -> Project interpreter to import the package, it is reportedModuleNotFoundErrorThis error:
insert image description here
Then directly win+R to the console to use pip install pytorchthe mling command, and the screen is full of red Error warnings. At this time, the blogger gradually discovered that the problem was not as simple as he thought.
insert image description here
I went to the pytorch official website https://pytorch.org/ to check the help. LibTorch is only applicable to C ++. The official website also suggested the method of downloading from GitHub, but these two methods are not as easy to use.
So currently only two methods of pip and conda are considered.
insert image description here

Install using the pip command

The pip method is the blogger's first consideration. After all, the Anaconda environment was not installed at the time, and the conda command could not be used, but when it was first used, it reported a strange error, so it had to be installed using conda. But in the end, the torch-1.4.0 version was successfully installed using pip , so first write this method in front of the conda command installation to speed up everyone's problem-solving speed.

first step: Note: It is recommended to update the pip version before using, the update command is as follows:
python -m pip install --upgrade pip

insert code snippet here
Or
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pip -U
(this command comes from the Tsinghua open source software mirror station pypi mirror usage help )

Step 2: Install and execute the command is very simple, enter the console with win+R and copy and paste the following command to install the latest version 1.4.0 of torch . (This time it was completed in one step, and the speed was extremely fast. It was 6.9M/s at the peak, which is unimaginable. I don’t understand why it kept reporting errors before?):

pip install torch===1.4.0 torchvision===0.5.0 -f https://download.pytorch.org/whl/torch_stable.html

(This command comes from the pytorch official website https://pytorch.org/ )

insert image description here
insert image description here
You're done!

The following figure conda listshows the result of using the command:
Use the conda list command to view the results
you can test it, and there is nothing wrong with it:
insert image description here

Install using the conda command

First, you need to install Anaconda or Miniconda. Anaconda is an open-source Python distribution, which includes more than 180 scientific packages such as conda and Python and their dependencies. Of course, it does not include pytorch, which needs to be downloaded separately.
The blogger is currently using Miniconda. Miniconda is a lightweight version of Anaconda. It is enough for general needs. Every time a python project is equipped with a running environment, it loads faster, and you can download any packages you need.
When installing Miniconda and Anaconda, you only need to follow the default steps, but there is a step suggested that you must ** click Add to environment variables **, so as not to have to do additional operations to add environment variables.
insert image description here
Anaconda download address: https://www.anaconda.com/distribution/
Minicoda download address: https://docs.conda.io/en/latest/miniconda.html

Start getting into the feature film! !

The conda environment is available, and the command is directly executed to download the torch package, but the progress is very slow and the direct Time Out is interrupted . : conda install pytorch torchvision cudatoolkit=10.1 -c pytorch(This command comes from pytorch official website https://pytorch.org/ )

Consider replacing domestic mirror sources. Commonly used mirror sites include Tsinghua University Open Source Mirror Site , China University of Science and Technology Open Source Mirror Site , and Alibaba Open Source Mirror Site .For specific commands, see:Add or change domestic mirror source channel

However, the torch image was not found using the Ali image source, and the Anaconda image of the University of Science and Technology of China stopped serving. The image source of the torch image is also directly linked to the image source of Tsinghua University, so here we mainly introduce the use of the image source of Tsinghua University.

Step 1: Change the image source of Tsinghua University Command:

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --set show_channel_urls yes

Step two:

The download speed is really fast, but I can only download the 1.2 and 1.3 versions of torch, and I cannot download the latest 1.4.0 version. Anyway, I haven’t downloaded it successfully. I always report an error in the middle of the slow download. It may be a problem with my computer.
Specific download command: (This command can quickly and successfully download torch1.2 version)

conda install pytorch torchvision cudatoolkit=10.0   -c pytorch

Summary : If you can use the pip method, use the pip method, which is simple, direct and easy to use.


Reference:
Pytorch official website
Tsinghua open source software mirror station Anaconda mirror use help
Tsinghua open source software mirror station pypi mirror use help
Chinese University of Science and Technology mirror source

Guess you like

Origin blog.csdn.net/TommyXu8023/article/details/105144772