Conda 创建和删除虚拟环境

2021-10-14
1、检验当前conda的版本 conda -V
C:\Users>conda -V
conda 4.10.1
2、conda 常用的命令
  • 查看已有的虚拟环境
C:\Users>conda env list
# conda environments:
#
base                  *  D:\APP\Anaconda3
clone                    D:\APP\Anaconda3\envs\clone
king                     D:\APP\Anaconda3\envs\king
opencv                   D:\APP\Anaconda3\envs\opencv
  • 创建虚拟环境 conda create -n env_name python=x.y
C:\Users>conda create -n json python=3.7
Collecting package metadata (current_repodata.json): done
Solving environment: done

==> WARNING: A newer version of conda exists. <==
  current version: 4.10.1
  latest version: 4.10.3

Please update conda by running

    $ conda update -n base -c defaults conda

## Package Plan ##

  environment location: D:\APP\Anaconda3\envs\json

  added / updated specs:
    - python=3.7


The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    ca-certificates-2021.9.30  |       haa95532_1         116 KB
    certifi-2021.10.8          |   py37haa95532_0         151 KB
    pip-21.2.4                 |   py37haa95532_0         1.8 MB
    python-3.7.11              |       h6244533_0        14.5 MB
    setuptools-58.0.4          |   py37haa95532_0         776 KB
    wincertstore-0.2           |   py37haa95532_2          15 KB
    ------------------------------------------------------------
                                           Total:        17.3 MB

The following NEW packages will be INSTALLED:

  ca-certificates    pkgs/main/win-64::ca-certificates-2021.9.30-haa95532_1
  certifi            pkgs/main/win-64::certifi-2021.10.8-py37haa95532_0
  openssl            pkgs/main/win-64::openssl-1.1.1l-h2bbff1b_0
  pip                pkgs/main/win-64::pip-21.2.4-py37haa95532_0
  python             pkgs/main/win-64::python-3.7.11-h6244533_0
  setuptools         pkgs/main/win-64::setuptools-58.0.4-py37haa95532_0
  sqlite             pkgs/main/win-64::sqlite-3.36.0-h2bbff1b_0
  vc                 pkgs/main/win-64::vc-14.2-h21ff451_1
  vs2015_runtime     pkgs/main/win-64::vs2015_runtime-14.27.29016-h5e58377_2
  wheel              pkgs/main/noarch::wheel-0.37.0-pyhd3eb1b0_1
  wincertstore       pkgs/main/win-64::wincertstore-0.2-py37haa95532_2

之后显示==Proceed ([y]/n)? ==,按y回车继续

Downloading and Extracting Packages
certifi-2021.10.8    | 151 KB    | ############################################################################ | 100%
python-3.7.11        | 14.5 MB   | ################################################################8            |  85%
...

这下载速度,我想吐槽了,是真的慢呀。不知道是不是我网速问题(⊙︿⊙)

  • 删除虚拟环境 conda remove -n env_name --all
C:\Users>conda remove -n king --all

Remove all packages in environment D:\APP\Anaconda3\envs\king:


## Package Plan ##

  environment location: D:\APP\Anaconda3\envs\king


The following packages will be REMOVED:

  certifi-2021.5.30-py36haa95532_0
  pip-21.0.1-py36haa95532_0
  python-3.6.13-h3758d61_0
  setuptools-52.0.0-py36haa95532_0
  sqlite-3.36.0-h2bbff1b_0
  vc-14.2-h21ff451_1
  vs2015_runtime-14.27.29016-h5e58377_2
  wheel-0.37.0-pyhd3eb1b0_1
  wincertstore-0.2-py36h7fe50ca_0


Proceed ([y]/n)? y

Preparing transaction: done
Verifying transaction: done
Executing transaction: done
  • 激活虚拟环境 conda activate env_name
C:\Users>conda activate clone

(clone) C:\Users>
  • 关闭虚拟环境 conda deactivate
(clone) C:\Users>conda deactivate

C:\Users>
  • 查看指定环境下已安装的package
    conda list -n env_name
C:\Users>conda list -n opencv
# packages in environment at D:\APP\Anaconda3\envs\opencv:
#
# Name                    Version                   Build  Channel
altgraph                  0.17.2                   pypi_0    pypi
certifi                   2021.5.30        py36haa95532_0
future                    0.18.2                   pypi_0    pypi
importlib-metadata        4.8.1                    pypi_0    pypi
numpy                     1.19.5                   pypi_0    pypi
opencv-python             4.5.3.56                 pypi_0    pypi
pefile                    2021.9.3                 pypi_0    pypi
pip                       21.0.1           py36haa95532_0
pyinstaller               4.5.1                    pypi_0    pypi
pyinstaller-hooks-contrib 2021.3                   pypi_0    pypi
python                    3.6.13               h3758d61_0
pywin32-ctypes            0.2.0                    pypi_0    pypi
setuptools                52.0.0           py36haa95532_0
sqlite                    3.36.0               h2bbff1b_0
typing-extensions         3.10.0.2                 pypi_0    pypi
vc                        14.2                 h21ff451_1
vs2015_runtime            14.27.29016          h5e58377_2
wheel                     0.37.0             pyhd3eb1b0_1
wincertstore              0.2              py36h7fe50ca_0
zipp                      3.5.0                    pypi_0    pypi
  • 安装指定环境下某个package
    conda install -n env_name [package]

  • 删除指定环境下某个package
    conda remove -n env_name [package]

  • 更新指定环境下某个package
    conda update -n env_name [package]

若已经进入某个环境中,需要操作以上操作,直接省略-n env_name即可。

3、其它

安装完anaconda后,发现每次打开中断后都会自动进入到base的虚拟环境中去。可以只有conda deactivate退出,野结衣关闭自动打开的虚拟环境,命令如下:
conda config --set auto_activate_base false

当然,我目前使用conda创建虚拟环境,是为了打包exe文件,如果直接用Python默认的环境打包exe文件,打包出来的文件会非常的庞大,所以,就需要用到虚拟环境了,在环境里安装指定的包,打包出来的exe文件会小很多。