Manage Python virtual environments created by Conda

Manage Python virtual environments created by Conda

I changed to a new computer (Windows 10) and plan to use Anaconda to manage my own packages.

Download Installation

Download Anaconda

I downloaded the latest version of Anaconda over the wall using UU cloud accelerated new membership benefits.

Install Anaconda

Install Anaconda directly to the target location.
At this time I found that Anaconda lost a lot of content in the installation directory. My criterion is: No\Script folder.

For security, download and install VS2019, check the python component and install it. Find the downloaded folder of VS2019 and move to the Anaconda installation location.

Next, create a new environment variable cloud.

New Virtual Environment

Open the command line and enter:

...>conda activate

Enter the underlying virtual environment (base), where the name appears at the beginning of the command line path
New virtual environment new_env1, enter:

(base) ...>conda create -n new_env1

If there is no -n command, then:

(base) ...>conda create new_env1
CondaValueError: The target prefix is the base prefix. Aborting.

Normally, it should show:

Collecting package metadata (current_repodata.json): done
Solving environment: done

## Package Plan ##

  environment location: %Anaconda Installation Directory%\envs\tmp



Proceed ([y]/n)?

The%Anaconda installation directory% refers to the installation directory of Anaconda, that is, the directory where the \Script folder is located.
Enter y to create the environment.

Change the location of the new virtual environment.

Open C:\Users\User name\condarc, where the user name is the current user's user name.
Add the following to.condarc:

channels:
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
  - defaults
show_channel_urls: true
envs_dirs:
  - %Custom Path%\conda\envs
  - %Anaconda Installation Directory%\envs
  - C:\Users\User name\.conda\envs
  - C:\Users\User name\AppData\Local\conda\conda\envs
pkgs_dirs:
  - %Custom Path%\conda\pkgs
  - %Anaconda Installation Directory%\pkgs
  - C:\Users\User name\.conda\pkgs
  - C:\Users\User name\AppData\Local\conda\conda\pkgs

Preservation.
The default.condarc file has only {} two characters. Delete and paste the above directly. If you're not sure, you can Input first

(base) ...>conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
(base) ...>conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
(base) ...>conda config --set show_channel_urls yes

To add channels and show_channel_urls variables.
You can now view the Anaconda environment information in the base environment:

(base) ...>conda activate new_env1
(new_env1) ...>conda info

Next, create another virtual environment, new_env2

(new_env1) ...>conda create -n new_env2

You can see that the environment location has changed, so you can safely enter y and Enter to create the environment.

Switching virtual environments

There is no difference between inside and outside a virtual environment, you can use commands such as conda [-h] [-V] command..., and commands such as ping can also be used, but the python interpreter is different.
It is worth mentioning that every time you use

(last_env) ...>conda activate next_env

To enter the next virtual environment next_env and use it again

conda deactivate

When the command exits, it exits to the last_env of the previous virtual environment, similar to stacking.

Adding packages to a virtual environment

Put up the trial and error process first

Download pygame installation package

First come to the environment new_env1 in the same directory as Anaconda and download pygame

(base) ...>conda new_env1
(new_env1) ...>conda install pygame
Collecting package metadata (current_repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.
Collecting package metadata (repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.

PackagesNotFoundError: The following packages are not available from current channels:

  - pygame

Current channels:

  - (URL)
  - 

To search for alternate channels that may provide the conda package you're
looking for, navigate to

    https://anaconda.org

and use the search bar at the top of the page.

(URL) Is conda's source server
Can't download, only use pip command.

(new_env1) ...>pip install pygame
Collecting pygame
  Downloading pygame-1.9.6-cp36-cp36m-win_amd64.whl (4.3 MB)

However, the network is not very good. The system does not show success until it has been downloaded several times.
To verify that it is only installed in the current virtual environment, go to the base environment to see:

(new_env1) ...>conda activate base
(base) pip list
Package                            Version
---------------------------------- ---------
...
pygame                             1.9.6
...

Clearly, pip is still a global command at this point.
So delete this package

(base) ...>pip uninstall pygame

Enter and delete successfully.
Then try again and again in the new_env2, base environment:

(base) ...>conda activate new_env2
(new_env2) ...>pip install pygame
Collecting pygame
  Using cached pygame-1.9.6-cp37-cp37m-win_amd64.whl (4.3 MB)
Installing collected packages: pygame
Successfully installed pygame-1.9.6

No downloads needed, but the pip commands are global. Searching for many solutions on the web is fruitless.

conda install command installation package

After the pip command is invalid, try installing other packages with the conda install command, such as matplotlib, in the environment new_env2 created in a custom path:

(new_env2) ...>conda install matplotlib
Collecting package metadata (current_repodata.json): done
Solving environment: done

## Package Plan ##

  environment location: %Custom Path%\conda\envs\new_env2

  added / updated specs:
    - matplotlib


The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    certifi-2019.11.28         |           py38_0         149 KB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
    cycler-0.10.0              |             py_2           9 KB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
    freetype-2.10.0            |       h563cfd7_1         476 KB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
    icu-64.2                   |       he025d50_1        14.1 MB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
    jpeg-9c                    |    hfa6e2cd_1001         314 KB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
    kiwisolver-1.1.0           |   py38he980bc4_0          61 KB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
    libclang-9.0.1             |default_hf44288c_0        20.8 MB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
    libpng-1.6.37              |       h7602738_0         1.3 MB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
    matplotlib-3.1.3           |           py38_0           7 KB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
    matplotlib-base-3.1.3      |   py38h2981e6d_0         6.5 MB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
    numpy-1.17.5               |   py38hc71023c_0         4.7 MB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
    pip-20.0.2                 |           py38_1         1.9 MB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
    pyparsing-2.4.6            |             py_0          59 KB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
    pyqt-5.12.3                |   py38h6538335_1         4.7 MB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
    python-3.8.1               |       he1f5543_2        18.9 MB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
    python-dateutil-2.8.1      |             py_0         220 KB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
    qt-5.12.5                  |       h7ef1ec2_0       104.4 MB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
    setuptools-45.1.0          |           py38_0         664 KB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
    six-1.14.0                 |           py38_0          23 KB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
    sqlite-3.30.1              |       hfa6e2cd_0         1.1 MB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
    tornado-6.0.3              |   py38hfa6e2cd_0         641 KB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
    vs2015_runtime-14.16.27012 |       hf0eaf9b_1         1.1 MB  defaults
    wheel-0.34.2               |           py38_0          61 KB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
    wincertstore-0.2           |        py38_1003          13 KB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
    zlib-1.2.11                |    h2fa13f4_1006         236 KB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
    ------------------------------------------------------------
                                           Total:       182.5 MB

The following NEW packages will be INSTALLED:

  ca-certificates    anaconda/cloud/conda-forge/win-64::ca-certificates-2019.11.28-hecc5488_0
  certifi            anaconda/cloud/conda-forge/win-64::certifi-2019.11.28-py38_0
  cycler             anaconda/cloud/conda-forge/noarch::cycler-0.10.0-py_2
  freetype           anaconda/cloud/conda-forge/win-64::freetype-2.10.0-h563cfd7_1
  icu                anaconda/cloud/conda-forge/win-64::icu-64.2-he025d50_1
  intel-openmp       pkgs/main/win-64::intel-openmp-2019.4-245
  jpeg               anaconda/cloud/conda-forge/win-64::jpeg-9c-hfa6e2cd_1001
  kiwisolver         anaconda/cloud/conda-forge/win-64::kiwisolver-1.1.0-py38he980bc4_0
  libblas            anaconda/cloud/conda-forge/win-64::libblas-3.8.0-14_mkl
  libcblas           anaconda/cloud/conda-forge/win-64::libcblas-3.8.0-14_mkl
  libclang           anaconda/cloud/conda-forge/win-64::libclang-9.0.1-default_hf44288c_0
  liblapack          anaconda/cloud/conda-forge/win-64::liblapack-3.8.0-14_mkl
  libpng             anaconda/cloud/conda-forge/win-64::libpng-1.6.37-h7602738_0
  matplotlib         anaconda/cloud/conda-forge/win-64::matplotlib-3.1.3-py38_0
  matplotlib-base    anaconda/cloud/conda-forge/win-64::matplotlib-base-3.1.3-py38h2981e6d_0
  mkl                pkgs/main/win-64::mkl-2019.4-245
  numpy              anaconda/cloud/conda-forge/win-64::numpy-1.17.5-py38hc71023c_0
  openssl            anaconda/cloud/conda-forge/win-64::openssl-1.1.1d-hfa6e2cd_0
  pip                anaconda/cloud/conda-forge/win-64::pip-20.0.2-py38_1
  pyparsing          anaconda/cloud/conda-forge/noarch::pyparsing-2.4.6-py_0
  pyqt               anaconda/cloud/conda-forge/win-64::pyqt-5.12.3-py38h6538335_1
  python             anaconda/cloud/conda-forge/win-64::python-3.8.1-he1f5543_2
  python-dateutil    anaconda/cloud/conda-forge/noarch::python-dateutil-2.8.1-py_0
  qt                 anaconda/cloud/conda-forge/win-64::qt-5.12.5-h7ef1ec2_0
  setuptools         anaconda/cloud/conda-forge/win-64::setuptools-45.1.0-py38_0
  six                anaconda/cloud/conda-forge/win-64::six-1.14.0-py38_0
  sqlite             anaconda/cloud/conda-forge/win-64::sqlite-3.30.1-hfa6e2cd_0
  tornado            anaconda/cloud/conda-forge/win-64::tornado-6.0.3-py38hfa6e2cd_0
  vc                 pkgs/main/win-64::vc-14.1-h0510ff6_4
  vs2015_runtime     pkgs/main/win-64::vs2015_runtime-14.16.27012-hf0eaf9b_1
  wheel              anaconda/cloud/conda-forge/win-64::wheel-0.34.2-py38_0
  wincertstore       anaconda/cloud/conda-forge/win-64::wincertstore-0.2-py38_1003
  zlib               anaconda/cloud/conda-forge/win-64::zlib-1.2.11-h2fa13f4_1006


Proceed ([y]/n)?

Enter to download.
Strangely, to download Python and pip, did you always use the Python interpreter in the base environment before? That's all right. Opening the installation directory of new_env2 is a lot more, and the installation directory of new_env1 is empty. My criterion is: there is no python.exe program file.
Then try downloading pygame again:

(new_env2) ...>pip install pygame
Collecting pygame
  Downloading pygame-1.9.6-cp36-cp36m-win_amd64.whl (4.3 MB)

Discovered that they need to download from the Internet again, the network is not in good condition, the download was interrupted, and the installation failed.
So go back to new_env1 and try again, this time download python:

  (new_env2) ...>conda activate new_env1
  (new_env1) ...>conda install conda install python
Collecting package metadata (current_repodata.json): done
Solving environment: done

## Package Plan ##

  environment location: %Custom Path%\conda\envs\new_env1

  added / updated specs:
    - python


The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    certifi-2019.11.28         |           py38_0         149 KB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
    pip-20.0.2                 |           py38_1         1.9 MB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
    python-3.8.1               |       he1f5543_2        18.9 MB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
    setuptools-45.1.0          |           py38_0         664 KB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
    vs2015_runtime-14.16.27012 |       hf0eaf9b_1         1.1 MB  defaults
    wheel-0.34.2               |           py38_0          61 KB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
    wincertstore-0.2           |        py38_1003          13 KB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
    ------------------------------------------------------------
                                           Total:        22.8 MB

The following NEW packages will be INSTALLED:

  ca-certificates    anaconda/cloud/conda-forge/win-64::ca-certificates-2019.11.28-hecc5488_0
  certifi            anaconda/cloud/conda-forge/win-64::certifi-2019.11.28-py38_0
  openssl            anaconda/cloud/conda-forge/win-64::openssl-1.1.1d-hfa6e2cd_0
  pip                anaconda/cloud/conda-forge/win-64::pip-20.0.2-py38_1
  python             anaconda/cloud/conda-forge/win-64::python-3.8.1-he1f5543_2
  setuptools         anaconda/cloud/conda-forge/win-64::setuptools-45.1.0-py38_0
  sqlite             anaconda/cloud/conda-forge/win-64::sqlite-3.30.1-hfa6e2cd_0
  vc                 pkgs/main/win-64::vc-14.1-h0510ff6_4
  vs2015_runtime     pkgs/main/win-64::vs2015_runtime-14.16.27012-hf0eaf9b_1
  wheel              anaconda/cloud/conda-forge/win-64::wheel-0.34.2-py38_0
  wincertstore       anaconda/cloud/conda-forge/win-64::wincertstore-0.2-py38_1003


Proceed ([y]/n)?


Downloading and Extracting Packages
pip-20.0.2           | 1.9 MB    | ######################################################################### | 100%
python-3.8.1         | 18.9 MB   | ######################################################################### | 100%
wincertstore-0.2     | 13 KB     | ######################################################################### | 100%
setuptools-45.1.0    | 664 KB    | ######################################################################### | 100%
wheel-0.34.2         | 61 KB     | ######################################################################### | 100%
certifi-2019.11.28   | 149 KB    | ######################################################################### | 100%
vs2015_runtime-14.16 | 1.1 MB    | ######################################################################### | 100%
Preparing transaction: done
Verifying transaction: done
Executing transaction: done

(new_env1) ...>pip install pygame
Collecting pygame
  Using cached pygame-1.9.6-cp37-cp37m-win_amd64.whl (4.3 MB)
Installing collected packages: pygame
Successfully installed pygame-1.9.6

Enter another environment to view,

(new_env1) ...>conda activate base
(base) ...>pip list

No pygame was found.

Install through downloaded packages

To install pyGame under new_env2 without downloading, find pygame-1.9.6-cp37-cp37m-win_amd64.whl in File Explorer. Paste into the%custom path%condapkgspython-3.6.7-he025d50_1005Libensurepipbundled You can then install pyGame directly.

Delete virtual environment

To ensure sufficient space on the system disk, delete the new_env1 environment:

(base) ...>conda remove -n game --all

Remove all packages in environment %Anaconda Installation Directory%\conda\envs\new_env1:


## Package Plan ##

  environment location: %Anaconda Installation Directory%\conda\envs\new_env1


The following packages will be REMOVED:

  ...


Proceed ([y]/n)? y

Preparing transaction: done
Verifying transaction: done
Executing transaction: done
Published 6 original articles. Praise 1. Visits 585
Private letter follow

Keywords: Anaconda Python pip JSON

Added by baccarak on Tue, 04 Feb 2020 04:54:03 +0200