Anaconda 有開源版和商業(yè)版社牲,這里只介紹 Anaconda distribution 開源版。
Anaconda 是由數(shù)據(jù)科學(xué)家開發(fā)别垮,并主要用于數(shù)據(jù)科學(xué)的python包和環(huán)境(context)管理程序恃逻。
1.Anaconda 提供的倉庫包含了超過fr 8,000 用于數(shù)據(jù)科學(xué)和機(jī)器學(xué)下的包均践。
- 它支持幾乎所有的操作系統(tǒng)和框架局装。
- Anaconda 支持快速的安裝坛吁,運(yùn)行和更新package. 它是以個(gè)環(huán)境管理器,支持快速的創(chuàng)建铐尚,保存拨脉,load,和切換環(huán)境塑径。
- Anaconda 提供桌面版工具,提供通過工具快速管理package填具。
安裝文檔地址:
Installation — Anaconda documentation
Anaconda Distribution 包含conda和Anaconda Navigator, 以及python 和很多用于科學(xué)計(jì)算的包文件. 安裝完 Anaconda, 這些都會(huì)自動(dòng)安裝统舀。
Anaconda 提供了命令行和界面客戶端(Navigator)兩種方式匆骗,兩者可以互操作。
命令行工具(Anaconda prompt)
- 檢查conda 安裝情況
> conda --version
conda 4.12.0 //當(dāng)前版本為4.12.0
- 管理環(huán)境
2.1 查看當(dāng)前所有環(huán)境列表
(base) C:\Users\34870>conda info --envs
# conda environments:
#
base * C:\ProgramData\Anaconda3 //當(dāng)前只創(chuàng)建了Anaconda3的默認(rèn)python環(huán)境
2.2 創(chuàng)建一個(gè)新環(huán)境
(base) C:\Users\34870>conda create --name monai_label
Collecting package metadata (current_repodata.json): done
Solving environment: done
## Package Plan ##
environment location: C:\Users\34870\.conda\envs\monai_label
Proceed ([y]/n)? y
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
#
# To activate this environment, use
#
# $ conda activate monai_label
#
# To deactivate an active environment, use
#
# $ conda deactivate
2.3 激活新環(huán)境
(monai_label) C:\Users\34870>conda info --envs
# conda environments:
#
base C:\ProgramData\Anaconda3
monai_label * C:\Users\34870\.conda\envs\monai_label
- 代表當(dāng)前默認(rèn)環(huán)境誉简, 可以看到新環(huán)境已經(jīng)切換到monai_label
2.4 切回默認(rèn)環(huán)境
(monai_label) C:\Users\34870>conda activate
(base) C:\Users\34870>conda info --envs
# conda environments:
#
base * C:\ProgramData\Anaconda3
monai_label C:\Users\34870\.conda\envs\monai_label
3 管理python
3.1 查看默認(rèn)環(huán)境python 版本
查看當(dāng)前環(huán)境的python 版本
(base) C:\Users\34870>python --version
Python 3.9.7
當(dāng)創(chuàng)建新環(huán)境的conda 會(huì)默認(rèn)安裝 下載并安裝Anaconda時(shí)的python 版本碉就。 如果需要不同的版本, 可以在創(chuàng)建新環(huán)境的時(shí)候闷串,確定一個(gè)python 版本瓮钥。
3.2 創(chuàng)建環(huán)境并確定安裝python 版本
conda create --name sky python=3.5
Collecting package metadata (current_repodata.json):
-----
執(zhí)行該命令會(huì)默認(rèn)安裝一些Python libs
安裝完后,激活環(huán)境并查看python版本:
python --version
Python 3.5.6 :: Anaconda, Inc.
3.3 刪除一個(gè)環(huán)境
(sky) C:\Users\34870>conda remove --name snakes --all
Remove all packages in environment C:\Users\34870\.conda\envs\snakes:
## Package Plan ##
environment location: C:\Users\34870\.conda\envs\snakes
The following packages will be REMOVED:
ca-certificates-2022.3.29-haa95532_0
certifi-2021.10.8-py39haa95532_2
openssl-1.1.1n-h2bbff1b_0
pip-21.2.4-py39haa95532_0
python-3.9.12-h6244533_0
setuptools-61.2.0-py39haa95532_0
sqlite-3.38.2-h2bbff1b_0
tzdata-2022a-hda174b7_0
vc-14.2-h21ff451_1
vs2015_runtime-14.27.29016-h5e58377_2
wheel-0.37.1-pyhd3eb1b0_0
wincertstore-0.2-py39haa95532_2
Proceed ([y]/n)? y
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
- python庫(package)管理
conda的一個(gè)主要作用就是庫管理烹吵,常會(huì)用以下的命令:
4.1 查詢當(dāng)前環(huán)境已經(jīng)安裝的package
>conda list
# packages in environment at C:\Users\34870\.conda\envs\sky:
#
# Name Version Build Channel
certifi 2020.6.20 pyhd3eb1b0_3
pip 10.0.1 py35_0
python 3.5.6 he025d50_0
setuptools 40.2.0 py35_0
vc 14.2 h21ff451_1
vs2015_runtime 14.27.29016 h5e58377_2
wheel 0.37.1 pyhd3eb1b0_0
wincertstore 0.2 py35hfebbdb8_0
4.2 在倉庫里查詢需要安裝的當(dāng)錢包是否存在
(sky) C:\Users\34870>conda search beautifulsoup4
Loading channels: done
# Name Version Build Channel
beautifulsoup4 4.6.0 py27_1 pkgs/main
beautifulsoup4 4.6.0 py27hc287451_1 pkgs/main
beautifulsoup4 4.6.0 py35_1 pkgs/main
beautifulsoup4 4.6.0 py35h61fcdcc_1 pkgs/main
beautifulsoup4 4.6.0 py36_1 pkgs/main
beautifulsoup4 4.6.0 py36hd4cc5e8_1 pkgs/main
beautifulsoup4 4.6.0 py37_1 pkgs/main
beautifulsoup4 4.6.1 py27_0 pkgs/main
4.3 安裝一個(gè)新包
(sky) C:\Users\34870>conda install beautifulsoup4
Collecting package metadata (current_repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.
Solving environment: failed with repodata from current_repodata.json, will retry with next repodata source.
Collecting package metadata (repodata.json): done
Solving environment: done
## Package Plan ##
environment location: C:\Users\34870\.conda\envs\sky
added / updated specs:
- beautifulsoup4
The following packages will be downloaded:
package | build
---------------------------|-----------------
beautifulsoup4-4.6.3 | py35_0 147 KB
------------------------------------------------------------
Total: 147 KB
The following NEW packages will be INSTALLED:
beautifulsoup4 pkgs/main/win-64::beautifulsoup4-4.6.3-py35_0
Proceed ([y]/n)? y
Downloading and Extracting Packages
beautifulsoup4-4.6.3 | 147 KB | ############################################################################# | 100%
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
conda的特點(diǎn)時(shí)會(huì)自動(dòng)將依賴也安裝上去碉熄。
- 創(chuàng)建一個(gè)指定路徑的環(huán)境
通常我們需要控制以下conda在指定目錄創(chuàng)建環(huán)境,并將包安裝到指定目錄肋拔,這時(shí)候我們需要用prefix確定路徑锈津。
5.1 在指定路徑創(chuàng)建一個(gè)環(huán)境
conda create --prefix /d/python-envs/monailable python=3.9
5.2 安裝完成后檢查新環(huán)境并激活
(base) C:\Users\34870>conda info --envs
# conda environments:
#
base * C:\ProgramData\Anaconda3
C:\d\python-envs\monailabel
(base) C:\Users\34870>conda activate C:\d\python-envs\monailabel
(C:\d\python-envs\monailabel) C:\Users\34870>conda info --envs
# conda environments:
#
base C:\ProgramData\Anaconda3
* C:\d\python-envs\monailabel
激活時(shí)需要使用前綴中注明的路徑。