Anaconda installs and uses PyTorch (PyCharm)

Article directory

  • Anaconda installs and uses PyTorch (PyCharm)
    • 1. Anaconda installation
      • 1.1 Download the installation package
      • 1.2 Installation
      • 1.3 Testing
      • 1.4 Change mirror source
    • 2. PyTorch installation
      • 2.1 Create a virtual environment
      • 2.3 Activate/deactivate the environment
      • 2.4 CUDA
      • 2.5 conda install PyTorch
    • 3. Use PyCharm
      • 3.1 Install PyCharm
      • 3.2 Login
      • 3.3 Using a virtual environment
    • Reference

Anaconda installs and uses PyTorch (PyCharm)

Preface

This tutorial is only applicable to: Windows system, Anaconda3-2023.03-Windows-x86_64, pycharm-professional-2023.1.2

Bug Guide

  • Check whether the Anaconda and PyChatm versions are consistent with the author? Is each operation consistent with the author?
  • If they are all consistent, post the question or bug in the comment area (reply as you please)!
  • Reinstalling Anaconda is 100% resolved. Before that, must clean up the environment or configuration according to Uninstalling Anaconda Distribution.

1. Anaconda installation

1.1 Download the installation package

When downloading the Anaconda installation package, there are often some questions, such as not knowing where to download, or downloading too slowly, etc. The author will provide the following two solutions.

(1) Official website download

Enter Anaconda official website, click Download to download the Windows version installer:

The above default downloads the latest version of Anaconda (which may cause configuration or compatibility bugs), click History Version for the historical version, as shown in the figure below:

Since it is downloaded from abroad, the speed will be slower, so this method is not recommended.

(2) Tsinghua Mirror

Enter the Tsinghua University open source software mirror site, select anaconda to enter the download directory of the software, and then click archive to choose which version to download:

The author downloaded: Anaconda3-2023.03-Windows-x86_64.exe

1.2 Installation

After downloading Anaconda, just double-click it. The following points need attention.

(1) Select All Users to apply to all users:

(2) Avoid installing on the C drive (it will cause the computer to slow down/card):

(3) Always default, The installation process lasts for about half an hour. . . The screenshot below indicates that the installation is complete, click Next:

(4) Click Next directly:

(5) Remove the two ticks to avoid opening the webpage and closing it, just Finish to complete the installation

1.3 Testing

After the Anaconda installation is complete, the following applications will be added:

Double-click Anaconda Prompt and enter the following command to test:

conda --version

If the output is as follows, the installation is successful:

conda 23.1.0

1.4 Change mirror source

When we use Conda to download and install the Python package, the default access to foreign servers is slow or an error is reported, so we need to change the source.

There are two ways to change the default source of Conda: (1) config directive; (2) modify the .condarac file.

(1)config

Enter the following command in Anaconda Prompt to add the Tsinghua image source:

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

At this time, open the user directory of the C disk (namely C:\Users\xxx, where xxx is your username), and you will find an additional .condarc file, Open it with Notepad like this:

Other actions:

  • Reset/delete mirror sources (or directly select and delete all in the .condarc file):
conda config --remove-key channels
  • If the mirror source contains defaults, it needs to be deleted, so that the Tsinghua mirror source will be used by default when downloading:
conda config --remove channels defaults

(2).condarc

Open the .condarc file with Notepad and add the following:

channels:
  - http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
  - http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
  - http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
  - http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
  - http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
  - http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/
  - http://mirrors.ustc.edu.cn/anaconda/cloud/msys2/
  - http://mirrors.ustc.edu.cn/anaconda/pkgs/free/
  - http://mirrors.ustc.edu.cn/anaconda/pkgs/main/
  - http://mirrors.ustc.edu.cn/anaconda/cloud/conda-forge/
  - http://mirrors.ustc.edu.cn/anaconda/cloud/menpo/
  - http://mirrors.ustc.edu.cn/anaconda/cloud/bioconda/
ssl_verify: true
show_channel_urls: true

2. PyTorch installation

Create a virtual environment called torch through Anaconda to facilitate the management of different versions of PyTorch.

2.1 Create a virtual environment

Conda creates a virtual environment in the user directory of the C drive by default (the author is: C:\Users\xxxxx\.conda\envs\\), the C drive will become bloated, this This causes the computer to slow down. But conda also offers to create an environment under a specified directory.

(1) Default directory

Create a virtual environment named torch in the default directory, python version is 3.8:

conda create -n torch python=3.8

(2) Specify the directory

Create a virtual environment named pytorch under D:\Software\envs\, the version of python is 3.10:

conda create --prefix D:\Software\envs\pytorch python=3.10

At this point, using conda env list does not fully display the virtual environment:

The newly added environment directory must be added to conda’s configuration using the following config command:

conda config --append envs_dirs D:\Software\envs

Use the following command again to view all virtual environments that have been created:

conda env list

2.3 Activate/deactivate the environment

When operating on a specified environment, you need to activate the virtual environment first; otherwise, exit the environment and return to the base environment.

(1) Activate the pytorch environment:

conda activate torch

(2) Exit (switch back to base):

conda deactivate

2.4 CUDA

Before officially installing PyTorch, we need to know the CUDA version of our computer, win + R to open and enter cmd, and then enter the following command:

nvidia-smi

Right now:

C:\Users\xxxxx>nvidia-smi
Sat May 20 11:17:37 2023
 + ------------------------------------------------- ---------------------------- +
| NVIDIA-SMI 512.89 Driver Version: 512.89 CUDA Version: 11.6 |
|------------------------------- + ----------------- ----- + ---------------------- +
| GPU Name TCC/WDDM | Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|=================================+ ================== ===== + =======================|
| 0 NVIDIA GeForce ... WDDM | 00000000:01:00.0 On | N/A |
| N/A 40C P3 N/A / N/A | 296MiB / 4096MiB | 0% Default |
| | | N/A |
 + ------------------------------- + ----------------- ----- + ---------------------- +

That is, the author’s CUDA version is 11.6, so the highest version of PyTorch can only be installed, and the version lower than 11.6 is also acceptable.

2.5 conda install PyTorch

Enter the historical version of PyTorch, find the instruction starting from # CUDA 11.6 (or a little lower, such as 11.3) and copy it to Anaconda Prompt.

Since the author created two virtual environments (torch and pytorch), the following demonstrates respectively:

Note: -c pytorch and -c pytorch -c conda-forge means Download from the official website, the speed is relatively slow, if you use the mirror source we added, just delete it.

conda activate torch # activate torch environment

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

#CUDA 11.6
conda install pytorch==1.12.1 torchvision==0.13.1 torchaudio==0.12.1 cudatoolkit=11.6 -c pytorch -c conda-forge

Test: If it is as follows, PyTorch is installed successfully

3. PyCharm use

3.1 Install PyCharm

Enter the official website to download PyCharm, the author downloaded the professional version (version: 2023.1.2):

Note:

  • The professional version has more complete functions and is suitable for professionals, but it can only be used for free for 30 days (you can find the cracked version online), students can refer to Reference [1] Submit for review (the cycle is about 1 month), and you can use it for free for one year after passing it (click License to query the license, as shown in the figure below), and you can apply again when it is about to expire.

  • The community version is free to use, but it seems that some functions are not available (the author is not clear).

Double-click the pycharm-professional-2023.1.2.exe file to execute the installer, and modify the installation location:

Check the following 3 options:

When started (running PyCharm):

3.2 Login

Entering PyCharm will pop up a license authentication window, and the author uses the JetBrains account (as mentioned above, students apply for a free trial for one year) to log in:

3.3 Using a virtual environment

Create a new Test folder on the random disk of the computer and open it with PyCharm:

Click Trust Project to trust the folder, and enter the main interface of PyCharm, which will automatically add a main.py file and load the environment for a period of time:

Click Python 3.10 in the lower right corner, click Add New Interpreter, select Add Local Interpreter...:

Refer to Reference [2], select Conda Environment, and then find conda.bat file, and then click Load Environment:

PyCharm will automatically load the created virtual environment:

Wait for the environment and dependent packages to be loaded, and you will find that the pytorch virtual environment is displayed in the lower right corner, and then enter the following code, Ctrl + Shift + F10 to execute:

You’re done! ! !

Reference

[1]: Still worrying about cracking IDEA/PyCharm/Clion? Students apply for the ultimate version of JetBrains education certification for free

[2]: The latest version of pycharm in 23 years cannot find the conda executable file solution