Conda 建立和刪除虛擬環境

echo沐雨兮發表於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檔案會小很多。

相關文章