Anaconda creates a virtual environment and uses the created environment in Pycharm

Anaconda creates a virtual environment and uses the created environment in Pycharm

  • 1. Anaconda creates a virtual environment
  • 2. Use the created environment in Pycharm
  • 3.2022.12.8 update

The advantage of Anaconda is that it can easily manage its own toolkit, development environment and Python version, and at the same time use different virtual environments to isolate projects with different requirements. If you have already installed Anaconda and Pycharm, this article will take you to quickly create a virtual environment and use it in Pycharm.

1.Anaconda creates a virtual environment

Step 1: Open Anaconda’s command line window (Anaconda Prompt)
Figure 1 Anaconda Prompt

Step 2: Enter the following command to create a virtual environment, and specify the python version you need when creating it. It is recommended that the python version not be too high.

conda create -n liuhaiwen python=3.7

Here I create a folder named liuhaiwen to store the environment I want to create, and specify to install python3.7. Then Anaconda Prompt will pop up the following information:

 Package Plan

  environment location: C:\ProgramData\Anaconda3\envs\liuhaiwen

  added / updated specs:
    -python=3.7


The following packages will be downloaded:

    package |
    --------------------------|-----------------
    sqlite-3.39.3 | h2bbff1b_0 1.2 MB
    certifi-2022.9.14 | py37haa95532_0 159 KB
    -------------------------------------------------- ----------
                                           Total: 1.4MB

The following NEW packages will be INSTALLED:

    ca-certificates: 2022.07.19-haa95532_0
    certifi: 2022.9.14-py37haa95532_0
    openssl: 1.1.1q-h2bbff1b_0
    pip: 22.1.2-py37haa95532_0
    python: 3.7.13-h6244533_0
    setuptools: 63.4.1-py37haa95532_0
    sqlite: 3.39.3-h2bbff1b_0
    vc: 14.2-h21ff451_1
    vs2015_runtime: 14.27.29016-h5e58377_2
    wheel: 0.37.1-pyhd3eb1b0_0
    wincertstore: 0.2-py37haa95532_2

Proceed ([y]/n)?

About two things are said, one is the directory of the environment to be created, which is generally under the disk installed by Anaconda by default, in ProgramData\Anaconda3\envs, and the second is to install some toolkits. Select y, the folder of the new environment will be created, and the required packages will be downloaded at the same time.

Downloading and Extracting Packages
sqlite-3.39.3 | 1.2 MB | ########################################### ###################################### | 100%
certifi-2022.9.14 | 159 KB | ########################################### ###################################### | 100%
Preparing transaction: done
Verifying transaction: done
Executing transaction: done

To activate this environment, use

$ conda activate liuhaiwen
#
#To deactivate an active environment, use
#
 $ conda deactivate

At this point, the virtual environment has been established.
Step 3: Activate the environment:

conda activate liuhaiwen
(liuhaiwen) C:\Users\Administrator>

Then we can continue to configure the toolkit we need in the created environment. You can use conda to install the toolkit, or you can use pip to install the toolkit (pip can install more packages), and install it according to your needs That’s it, we install a deep learning framework Pytorch here.
Step 4: Install Pytorch
Execute the following command to install Pytorch:

conda install pytorch

If you want to install the GPU version of Pytorch, you can go to the official website of Pytorch to find the download link of the corresponding version. I use CUDA10.1:

# CUDA 10.1 + Windows
pip install torch==1.8.1 + cu101 torchvision==0.9.1 + cu101 torchaudio==0.8.1 -f https://download.pytorch.org/whl/torch_stable.html

2022.11.09 Switch to CUDA 11.3

#CUDA 11.3 + Windows
conda install pytorch==1.12.1 torchvision==0.13.1 torchaudio==0.12.1 cudatoolkit=11.3 -c pytorch
The following NEW packages will be INSTALLED:

    blas: 1.0-mkl
    cffi: 1.15.1-py37h2bbff1b_0
    future: 0.18.2-py37_1
    intel-openmp: 2021.4.0-haa95532_3556
    libuv: 1.40.0-he774522_0
    mkl: 2021.4.0-haa95532_640
    mkl-service: 2.4.0-py37h2bbff1b_0
    mkl_fft: 1.3.1-py37h277e83a_0
    mkl_random: 1.2.2-py37hf11a4ad_0
    ninja: 1.10.2-haa95532_5
    ninja-base: 1.10.2-h6d14046_5
    numpy: 1.21.5-py37h7a0a035_3
    numpy-base: 1.21.5-py37hca35cd5_3
    pycparser: 2.21-pyhd3eb1b0_0
    pytorch: 1.10.2-cpu_py37h907fbb5_0
    six: 1.16.0-pyhd3eb1b0_1
    typing-extensions: 4.3.0-py37haa95532_0
    typing_extensions: 4.3.0-py37haa95532_0

Proceed ([y]/n)?

The above is Pytorch and its required dependent packages. Just select y and start the installation. This is a major advantage of Anaconda (automatic installation of dependent packages).

Downloading and Extracting Packages
pytorch-1.10.2 | 200.0 MB | ########################################### ###################################### | 100%
future-0.18.2 | 746 KB | ########################################### ###################################### | 100%
Preparing transaction: done
Verifying transaction: done
Executing transaction: done

(liuhaiwen) C:\Users\Administrator>

Pytorch installed successfully.

2. Use the created environment in Pycharm

Step 1: Open Pycharm, create a project, and select previously configured interpreter.

Step 2: Select Existing environment in Conda Environment, then select python.exe in the environment just created in Interpreter, and click ok.

Step 3: To test the Pytorch environment, we use pytorch to create a Tensor.

import torch
a=torch.tensor([[1.0,2.0],[3.0,4.0]])
print(a)

Output:

tensor([[1., 2.],
        [3., 4.]])

So far, the Pytorch virtual environment has been created and successfully used in Pycharm.

3.2022.12.8 update

There is generally no problem with the environment installed by conda (it has been used for a long time), but I encountered a mismatch between torch and torchvision versions in the process of using this environment recently.
If you also encounter it, I recommend pip installation!
The cuda11.3pip installation command is as follows:

pip install torch==1.12.1 + cu113 torchvision==0.13.1 + cu113 torchaudio==0.12.1 --extra-index-url https://download.pytorch.org/whl/cu113

Please check another article for details: https://blog.csdn.net/qq_45160840/article/details/128244733