How to set up Pytorch environment (Windows version)

Since the CUDA Version has been updated to 11.7, this tutorial has also been updated simultaneously.

1 Install Anaconda

(1) First open the Anaconda official website and download the installation package for the corresponding platform
Anaconda official website
insert image description here
insert image description here
. The package we installed here is Anaconda3-2022.10-Windows-x86_64.exe.
Then, double-click the exe file to start the installation
and wait for the installation to complete (select Just me here)

insert image description here
insert image description here
Note: There is a way to automatically add environment variables. During the installation process, just check the Automatically add to Path option! Do this to skip step (2) below
insert image description here

(2) After the installation is completed, click System Properties Settings - Add Environment Variables (you can skip this step if you check the Automatically add to Path option during installation) :
insert image description here

Add under the system Path path (the two lines in the highlighted part, the specific installation path is subject to the machine):
\Anaconda3\Scripts
\Anaconda3\Library\bin

insert image description here

(3) Open the CMD command and enter conda. If it can be displayed normally, it means the installation has been successful:
insert image description here

2 Install Pytorch

(1) Initialize the .condarc file

conda config --set show_channel_urls yes

insert image description here
At this time, when we open the C drive user, we can see an additional .condarc file
insert image description here
(2) Open this file and edit it in the following way:
first set the root directory of the virtual environment

envs_dirs:
  - D:\software\Anaconda3\envs

Then add channels:
Tsinghua University mirror (installation is fast, but sometimes the latest version may not be installed successfully)

channels:
  - defaults
show_channel_urls: true
channel_alias: https://mirrors.tuna.tsinghua.edu.cn/anaconda
default_channels:
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/pro
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
custom_channels:
  conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud

Alibaba Cloud Image (slow installation speed, but complete package content)

channels:
  - defaults
show_channel_urls: true
default_channels:
  - http://mirrors.aliyun.com/anaconda/pkgs/main
  - http://mirrors.aliyun.com/anaconda/pkgs/r
  - http://mirrors.aliyun.com/anaconda/pkgs/msys2
custom_channels:
  conda-forge: http://mirrors.aliyun.com/anaconda/cloud
  msys2: http://mirrors.aliyun.com/anaconda/cloud
  bioconda: http://mirrors.aliyun.com/anaconda/cloud
  menpo: http://mirrors.aliyun.com/anaconda/cloud
  pytorch: http://mirrors.aliyun.com/anaconda/cloud
  simpleitk: http://mirrors.aliyun.com/anaconda/cloud

A combined version of the two

channels:
  - defaults
show_channel_urls: true
channel_alias: https://mirrors.tuna.tsinghua.edu.cn/anaconda
default_channels:
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/pro
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
  - http://mirrors.aliyun.com/anaconda/pkgs/main
  - http://mirrors.aliyun.com/anaconda/pkgs/r
  - http://mirrors.aliyun.com/anaconda/pkgs/msys2
custom_channels:
  conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud

(3) Create a virtual python environment for pytorch: (Open the cmd command line window and add the specified installation path after prefix)

conda create -n pytorch python=3.9 

insert image description here
Enter y to start creating the virtual environment.
After creation, activate the virtual environment and install pytorch:

conda activate pytorch
conda install pytorch==1.12.1 torchvision==0.13.1 torchaudio==0.12.1 cudatoolkit=11.3 -c pytorch

After the installation is completed, you can use the pytorch environment
insert image description here
(4) Check whether the pytorch environment is installed successfully

import torch
print(torch.cuda.is_available())
True

Guess you like

Origin blog.csdn.net/weixin_41801682/article/details/125630558