Anaconda常見使用方法


0.Anaconda介紹

Anaconda指的是一個開源的Python發(fā)行版本,包含了許多工具包,除了基本的python和pip,還有做CV的opencv包,web開發(fā)的web包,與matlab畫圖功能相同的matplotlib包以及AI和機器學習會用到的numpy、tensorflow和pytorch。
除此之外,Anaconda有命令行模式prompt與圖形界面navigator,剛上手的時候感覺圖像界面真香,慢慢用著要求越來越多的時候發(fā)現(xiàn)命令行模式無論在速度上還是功能上都要比圖形界面好很多。本文會以命令行模式為例。
Anaconda比起其他IDE來說不算笨重,創(chuàng)建和管理環(huán)境很方便,Anaconda占內(nèi)存會越來越大是因為下載的安裝包越來越多,只要將pkgs中不需要的東西刪掉體積就會小很多。
在Anaconda平臺上集成了VS code、jupyter notenook和spyder這些工具,都可以用來編譯和調(diào)試,可以選擇性的安裝。


1.Anaconda環(huán)境


1.1why創(chuàng)建不同環(huán)境

初用者肯定會有這樣的疑問,在base環(huán)境下把所有需要用的包都裝上去不就可以了,為什么還要創(chuàng)建那么多不同的環(huán)境?
如果所有安裝包需要的版本號都是兼容的,確實可以這樣做,可是實際上不同的包對其他包的版本依賴是各不相同的,所以需要創(chuàng)建不同的環(huán)境來使用這些工具包。


1.2默認路徑管理環(huán)境

1.2.1創(chuàng)建環(huán)境

(base) C:\Users\dell>conda create -n my_env python=3.7

-n后跟環(huán)境名稱
python=3.7選擇python的版本號,如果不加默認安裝最新版本
也可以在創(chuàng)建環(huán)境時指定安裝包,numpy為一個安裝包的名字

conda create -n my_env numpy

如果需要更多的功能,可以查看,其他命令也可如此

conda create -h

創(chuàng)建環(huán)境時會有如下提示

(base) C:\Users\dell>conda create -n my_env python=3.7
Collecting package metadata (repodata.json): done
Solving environment: \
Warning: >10 possible package resolutions (only showing differing packages):
  - https://mirrors.ustc.edu.cn/anaconda/pkgs/main/win-64::certifi-2019.11.28-py37_0, https://mirrors.ustc.edu.cn/anaconda/pkgs/main/win-64::setuptools-45.2.0-py37_0, https://mirrors.ustc.edu.cn/anaconda/pkgs/main/win-64::wheel-0.34.2-py37_0, https://mirrors.ustc.edu.cn/anaconda/pkgs/main/win-64::wincertstore-0.2-py37_0
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/win-64::wincertstore-0.2-py37_0, https://mirrors.ustc.edu.cn/anaconda/pkgs/main/win-64::certifi-2019.11.28-py37_0, https://mirrors.ustc.edu.cn/anaconda/pkgs/main/win-64::setuptools-45.2.0-py37_0, https://mirrors.ustc.edu.cn/anaconda/pkgs/main/win-64::wheel-0.34.2-py37_0
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/win-64::certifi-2019.11.28-py37_0, https://mirrors.ustc.edu.cn/anaconda/pkgs/main/win-64::setuptools-45.2.0-py37_0, https://mirrors.ustc.edu.cn/anaconda/pkgs/main/win-64::wheel-0.34.2-py37_0, https://mirrors.ustc.edu.cn/anaconda/pkgs/main/win-64::wincertstore-0.2-py37_0
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/win-64::certifi-2019.11.28-py37_0, https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/win-64::wincertstore-0.2-py37_0, https://mirrors.ustc.edu.cn/anaconda/pkgs/main/win-64::setuptools-45.2.0-py37_0, https://mirrors.ustc.edu.cn/anaconda/pkgs/main/win-64::wheel-0.34.2-py37_0
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/win-64::certifi-2019.11.28-py37_0, https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/win-64::wheel-0.34.2-py37_0, https://mirrors.ustc.edu.cn/anaconda/pkgs/main/win-64::setuptools-45.2.0-py37_0, https://mirrors.ustc.edu.cn/anaconda/pkgs/main/win-64::wincertstore-0.2-py37_0
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/win-64::certifi-2019.11.28-py37_0, https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/win-64::wheel-0.34.2-py37_0, https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/win-64::wincertstore-0.2-py37_0, https://mirrors.ustc.edu.cn/anaconda/pkgs/main/win-64::setuptools-45.2.0-py37_0
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/win-64::wheel-0.34.2-py37_0, https://mirrors.ustc.edu.cn/anaconda/pkgs/main/win-64::certifi-2019.11.28-py37_0, https://mirrors.ustc.edu.cn/anaconda/pkgs/main/win-64::setuptools-45.2.0-py37_0, https://mirrors.ustc.edu.cn/anaconda/pkgs/main/win-64::wincertstore-0.2-py37_0
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/win-64::wheel-0.34.2-py37_0, https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/win-64::wincertstore-0.2-py37_0, https://mirrors.ustc.edu.cn/anaconda/pkgs/main/win-64::certifi-2019.11.28-py37_0, https://mirrors.ustc.edu.cn/anaconda/pkgs/main/win-64::setuptools-45.2.0-py37_0
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/win-64::setuptools-45.2.0-py37_0, https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/win-64::wheel-0.34.2-py37_0, https://mirrors.ustc.edu.cn/anaconda/pkgs/main/win-64::certifi-2019.11.28-py37_0, https://mirrors.ustc.edu.cn/anaconda/pkgs/main/win-64::wincertstore-0.2-py37_0
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/win-64::setuptools-45.2.0-py37_0, https://mirrors.ustc.edu.cn/anaconda/pkgs/main/win-64::certifi-2019.11.28-py37_0, https://mirrors.ustc.edu.cn/anaconda/pkgs/main/win-64::wheel-0.34.2-py37_0, https://mirrors.ustc.edu.cn/anaconda/pkgs/main/win-64::wincertstore-0.2-py37_0
  ... and othedone

## Package Plan ##

  environment location: C:\Users\dell\Anaconda3\envs\my_env

  added / updated specs:
    - python=3.7


The following NEW packages will be INSTALLED:

  ca-certificates    anaconda/pkgs/main/win-64::ca-certificates-2020.1.1-0
  certifi            anaconda/pkgs/main/win-64::certifi-2019.11.28-py37_0
  openssl            anaconda/pkgs/main/win-64::openssl-1.1.1d-he774522_4
  pip                anaconda/pkgs/main/win-64::pip-20.0.2-py37_1
  python             anaconda/pkgs/main/win-64::python-3.7.6-h60c2a47_2
  setuptools         anaconda/pkgs/main/win-64::setuptools-45.2.0-py37_0
  sqlite             anaconda/pkgs/main/win-64::sqlite-3.31.1-he774522_0
  vc                 anaconda/pkgs/main/win-64::vc-14.1-h0510ff6_4
  vs2015_runtime     anaconda/pkgs/main/win-64::vs2015_runtime-14.16.27012-hf0eaf9b_1
  wheel              anaconda/pkgs/main/win-64::wheel-0.34.2-py37_0
  wincertstore       anaconda/pkgs/main/win-64::wincertstore-0.2-py37_0


Proceed ([y]/n)? y

Preparing transaction: done
Verifying transaction: done
Executing transaction: done
#
# To activate this environment, use
#
#     $ conda activate my_env
#
# To deactivate an active environment, use
#
#     $ conda deactivate

resolutions后面跟的是下載路徑,由于我添加了幾個國內(nèi)的鏡像,所以下載地址為清華、中科大和阿里云的鏡像。package plan后面會顯示下載包的信息,由于我的/pkgs文件夾中已經(jīng)有下載過的這些包了,所以不會再下載。這些安裝包是這個環(huán)境基礎的包。解讀提示信息當安裝出bug/bugs的時候是很重要的,可以幫助你很快的定位到出錯的源頭,即使解決不了,也不用浪費時間瞎忙活。
建立環(huán)境后可以查看以下當前的環(huán)境,可以看到my_env已經(jīng)建立,*指向當前所在環(huán)境

(base) C:\Users\dell>conda info --envs
# conda environments:
#
base                  *  C:\Users\dell\Anaconda3
OpenCV                   C:\Users\dell\Anaconda3\envs\OpenCV
my_env                   C:\Users\dell\Anaconda3\envs\my_env
tensorflow_2             C:\Users\dell\Anaconda3\envs\tensorflow_2
web                      C:\Users\dell\Anaconda3\envs\web
                         E:\local\pytorch

1.2.2進入環(huán)境/激活環(huán)境

用以下命令,my_env為環(huán)境名字

(base) C:\Users\dell>conda activate my_env
(my_env) C:\Users\dell>

1.2.3退出環(huán)境

(my_env) C:\Users\dell>conda deactivate
(base) C:\Users\dell>

1.2.4刪除環(huán)境

注意刪除my_env時要先退出my_env

(base) C:\Users\dell>conda remove -n my_env --all


1.3指定路徑管理環(huán)境

1.3.1創(chuàng)建環(huán)境

E:\local\my_env_2為環(huán)境路徑

(base) C:\Users\dell>conda create --prefix=E:\local\my_env_2 python=3.7

此時所有環(huán)境為

base                  *  C:\Users\dell\Anaconda3
OpenCV                   C:\Users\dell\Anaconda3\envs\OpenCV
my_env                   C:\Users\dell\Anaconda3\envs\my_env
tensorflow_2             C:\Users\dell\Anaconda3\envs\tensorflow_2
web                      C:\Users\dell\Anaconda3\envs\web
                         E:\local\my_env_2
                         E:\local\pytorch

發(fā)現(xiàn)新創(chuàng)建的環(huán)境沒有名字,要通過路徑來訪問

1.3.2激活環(huán)境

(base) C:\Users\dell>conda activate E:\local\my_env_2
(E:\local\my_env_2) C:\Users\dell>

1.3.3刪除環(huán)境

(base) C:\Users\dell>conda remove --prefix=E:\local\my_env_2 --all


2.安裝包


2.0設置鏡像源

可以添加多個鏡像源,中科大源、清華源、阿里源都挺好用

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge 
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main 
conda config --set show_channel_urls yes

pip設置鏡像源可以在pip.ini中修改,我的pip.ini位于C:\Users\dell\AppData\Roaming\pip,配置了阿里云的鏡像

[global]
index-url=http://mirrors.aliyun.com/pypi/simple/
 
[install]
trusted-host=mirrors.aliyun.com


2.1升級包的版本

運行以下命令可以將python包升級到最新版本

(base) C:\Users\dell>conda update -n my_env python


2.2安裝包

2.2.1conda安裝

可以指定安裝包的版本號,也可以不指定,默認安裝最新版本,需要在相關網(wǎng)站上根據(jù)自己的硬件配置選擇合適的版本號

(base) C:\Users\dell>conda install tensorflow=1.13.1

2.2.2pip安裝

(base) C:\Users\dell>pip install tensorflow==1.13.1

2.2.3安裝包列表

(base) C:\Users\dell>conda list

2.2.4刪除緩存中無用包

(base) C:\Users\dell>conda clean -p
(base) C:\Users\dell>conda clean -t


2.3刪除包

指定環(huán)境中刪除包(pip)

(base) C:\Users\dell>conda remove -n my_env pip


2.4安裝jupyter、spyder

(my_env) C:\Users\dell>pip install jupyter
(my_env) C:\Users\dell>pip install spyder


3.anaconda navigator


3.0介紹

navigator中可以創(chuàng)建刪除環(huán)境,搜索安裝包,指定安裝包的版本,在指定環(huán)境中安裝并打開jupyter等

navigator界面


3.1管理環(huán)境

進入環(huán)境界面可以看到當前所在環(huán)境和環(huán)境列表,下方紅色框選項為創(chuàng)建、克隆、導入和刪除環(huán)境

envs


3.2安裝包

圖中下拉列表可以查看已安裝未安裝的包,channel可以添加源,upadate可以更新資源,搜索框可以查找需要的包。包的右端為版本號,藍色標志為可以更新的包。

pkgs

右鍵選中包,可以選擇刪除或安裝指定版本
pkg_version


3.3jupyter、spyder

選擇環(huán)境后依次點擊install、launch即可使用jupyter和spyder

launch


最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內(nèi)容