**Author By Bing **
On 2020-08-06
[toc]
1. Anaconda簡介
1.1 特點
Anaconda是Python的包管理器和環(huán)境管理器,其包含了conda、Python等180多個科學(xué)包及其依賴項。
我已經(jīng)安裝了Python,那么為什么還需要Anaconda呢?原因有以下幾點:
- Anaconda附帶了一大批常用數(shù)據(jù)科學(xué)包,它附帶了conda、Python和 180 多個科學(xué)包及其依賴項,因此你可以用Anaconda立即開始處理數(shù)據(jù)。
- 包管理:Anaconda 是在 conda(一個包管理器和環(huán)境管理器)上發(fā)展出來的。在數(shù)據(jù)分析中,你會用到很多第三方的包,而conda(包管理器)可以很好的幫助你在計算機上安裝和管理這些包,包括安裝、卸載和更新包。
- 環(huán)境管理:為什么需要管理環(huán)境呢?比如你在A項目中用到了Python2,而新的項目要求使用Python3,而同時安裝兩個Python版本可能會造成許多混亂和錯誤。這時候conda就可以幫助你為不同的項目建立不同的運行環(huán)境。還有很多項目使用的包版本不同,比如不同的pandas版本,不可能同時安裝兩個pandas版本。你要做的應(yīng)該是在項目對應(yīng)的環(huán)境中創(chuàng)建對應(yīng)的pandas版本。這時候conda就可以幫你做到。
1.2 下載
官網(wǎng)地址:https://www.anaconda.com/download/
官方archive: https://repo.anaconda.com/archive/ (推薦,全)
清華鏡像:https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/(推薦,快)
1.3 Anaconda 安裝包版本與Python版本對應(yīng)關(guān)系
| Release date | conda | python2.7 | python3.4 | python3.5 | python3.6 | python3.7 | python3.8 |
|---|---|---|---|---|---|---|---|
| 2015/2/25 | 3.4.3 | ||||||
| 2015/5/23 | 2.7.10 | ||||||
| 2015/9/13 | 3.5.0 | ||||||
| 2015/12/5 | 2.7.11 | ||||||
| 2015/12/7 | 3.5.1 | ||||||
| 2015/12/21 | 3.4.4 | ||||||
| 2016/3/29 | conda 4.0.5 | Anaconda2-4.0.0 | Anaconda3-4.0.0 | ||||
| (python 2.7.11) | (python 3.5.1) | ||||||
| 2016/6/28 | conda 4.1.4 | Anaconda2-4.1.0 | Anaconda3-4.1.0 | ||||
| (python 2.7.11) | (python 3.5.1) | ||||||
| 2016/6/25 | 2.7.12 | ||||||
| 2016/6/27 | 3.4.5 | 3.5.2 | |||||
| 2016/7/8 | conda 4.1.6 | Anaconda2-4.1.1 | Anaconda3-4.1.1 | ||||
| (python 2.7.12) | (python 3.5.2) | ||||||
| 2016/9/28 | conda 4.2.9 | Anaconda2-4.2.0 | Anaconda3-4.2.0 | ||||
| (python 2.7.12) | (python 3.5.2) | ||||||
| 2016/12/17 | 2.7.13 | ||||||
| 2016/12/23 | 3.6.0 | ||||||
| 2017/1/17 | 3.4.6 | 3.5.3 | |||||
| 2017/1/31 | conda 4.3.8 | Anaconda2-4.3.0 | Anaconda3-4.3.0 | ||||
| (python 2.7.13) | (python 3.6.0) | ||||||
| 2017/3/10 | conda 4.3.14 | Anaconda2-4.3.1 | Anaconda3-4.3.1 | ||||
| (python 2.7.13) | (python 3.6.0) | ||||||
| 2017/3/21 | 3.6.1 | ||||||
| 2017/5/31 | conda 4.3.21 | Anaconda2-4.4.0 | Anaconda3-4.4.0 | ||||
| (python 2.7.13) | (python 3.6.1) | ||||||
| 2017/7/17 | 3.6.2 | ||||||
| 2017/8/8 | 3.5.4 | ||||||
| 2017/8/9 | 3.4.7 | ||||||
| 2017/9/26 | conda 4.3.27 | Anaconda2-5.0.0 | Anaconda3-5.0.0 | ||||
| (python 2.7.13) | (python 3.6.2) | ||||||
| 2017/9/16 | 2.7.14 | ||||||
| 2017/10/3 | 3.6.3 | ||||||
| 2017/10/25 | conda 4.3.30 | Anaconda2-5.0.1 | Anaconda3-5.0.1 | ||||
| (python 2.7.14) | (python 3.6.3) | ||||||
| 2017/12/19 | 3.6.4 | ||||||
| 2018/2/5 | 3.4.8 | 3.5.5 | |||||
| 2018/2/15 | conda 4.4.10 | Anaconda2-5.1.0 | Anaconda3-5.1.0 | ||||
| (python 2.7.14) | (python 3.6.4) | ||||||
| 2018/3/28 | 3.6.5 | ||||||
| 2018/5/1 | 2.7.15 | ||||||
| 2018/5/30 | conda 4.5.4 | Anaconda2-5.2.0 | Anaconda3-5.2.0 | ||||
| (python 2.7.15) | (python 3.6.5) | ||||||
| 2018/6/27 | 3.6.6 | 3.7.0 | |||||
| 2018/8/2 | 3.4.9 | 3.5.6 | |||||
| 2018/9/28 | conda 4.5.11 | Anaconda2-5.3.0 | Anaconda3-5.3.0 | ||||
| (python 2.7.15) | (python 3.7.0) | ||||||
| 2018/10/20 | 3.6.7 | 3.7.1 | |||||
| 2018/12/21 | conda 4.5.12 | Anaconda2-2018.12 | Anaconda3-2018.12 | ||||
| (python 2.7.15) | (python 3.7.1) | ||||||
| 2018/12/24 | 3.6.8 | 3.7.2 | |||||
| 2019/3/4 | 2.7.16 | ||||||
| 2019/3/18 | 3.4.10 | 3.5.7 | |||||
| 2019/3/25 | 3.7.3 | ||||||
| 2019/4/4 | conda 4.6.11 | Anaconda2-2019.03 | Anaconda3-2019.03 | ||||
| (python 2.7.16) | (python 3.7.3) | ||||||
| 2019/7/2 | 3.6.9 | ||||||
| 2019/7/8 | 3.7.4 | ||||||
| 2019/7/24 | conda 4.7.10 | Anaconda2-2019.07 | Anaconda3-2019.07 | ||||
| (python 2.7.16) | (python 3.7.3) | ||||||
| 2019/10/14 | 3.8.0 | ||||||
| 2019/10/15 | 3.7.5 | ||||||
| 2019/10/19 | 2.7.17 | ||||||
| 2019/10/29 | 3.5.8 | ||||||
| 2019/11/2 | 3.5.9 |
1.4 安裝
略
Note:注意添加環(huán)境變量
1.5 軟件簡介
-
Jupyter Lab
-
Jupyter Notebook-REPL
-
Spyder-IDE
1.6 pip 與 conda之爭
配置源的目的:官方服務(wù)器在國外,下載速度慢。配置源可以加速下載所需要的軟件包。
配置pip源
臨時源
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pandas-
固定源
- 類Unix
cd mkdir .pip # 注意有個點 vim pip.conf # 寫入以下內(nèi)容 [global] index-url = https://pypi.tuna.tsinghua.edu.cn/simple [install] trusted-host = https://pypi.tuna.tsinghua.edu.cn- Windows
# 在user目錄下新建一個pip目錄,注意沒有“點”。然后新建pip.ini文件,寫入以下內(nèi)容 [global] index-url = http://pypi.douban.com/simple [install] trusted-host = pypi.douban.com
配置conda源
-
臨時源
- 這個不太常用,沒試過,以后試過再來補充。
-
固定源
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 -
換回默認源
conda config --remove-key channels
在執(zhí)行conda config 命令的時候,會在當(dāng)前用戶目錄下創(chuàng)建.condarc文件,注意
pip VS conda

https://www.sohu.com/a/259103573_657064
http://www.itdecent.cn/p/5601dab5c9e5
小孩子才做選擇,成年人的我全都要。
將pip與conda結(jié)合的主要原因是有些包只能通過pip安裝。 Anaconda創(chuàng)酷提供超過1,500個軟件包,包括最流行的數(shù)據(jù)科學(xué),機器學(xué)習(xí)和AI框架。這些,以及包括conda-forge和bioconda在內(nèi)的數(shù)據(jù)通過Anaconda云提供的數(shù)千個附加軟件包,可以使用conda進行安裝。盡管有大量的軟件包,但與PyPI上提供的150,000多個軟件包相比,它仍然很小。有時候需要的包沒有conda包,但在PyPI上有,可以用pip安裝。
2. 虛擬環(huán)境
2.1 使用conda管理虛擬環(huán)境
- 創(chuàng)建虛擬環(huán)境
conda create -n xb_py_venv
之后y確認即可。
- 激活(脫出)虛擬環(huán)境
source activate xb_py_venv #激活
source deactivate xb_py_venv #不激活
其他關(guān)于conda的命令
列出所有已有虛擬環(huán)境
conda env list
conda info -e
- 創(chuàng)建新的虛擬環(huán)境
conda create -n env_name python=version
- 激活并進入虛擬環(huán)境
conda activate env_name
- 刪除一個已有的虛擬環(huán)境(以下兩條指令均可)
conda env remove -n env_name
- 分享代碼的時候,同時也需要將運行環(huán)境分享給大家
conda env export > env.yaml
- 用對方分享的YAML文件來創(chuàng)建一模一樣的運行環(huán)境
conda env create -f env.yaml
- Jupyter 運行Anaconda的虛擬環(huán)境
source activate env_name
conda install ipykernel (注意:在虛擬環(huán)境中安裝ipykernel)
python -m ipykernel install --name env_name --display-name "env_name" (寫入Jupyter的kernel中)
jupyter notebook
- 管理包
- 管理指定虛擬環(huán)境的包
conda install package_name -n env_name
conda install package_name
conda install pack=version (指定安裝包的版本)
- 刪除包
conda remove package_name
- 更新包
conda update package_name
- 列出所有已安裝的包
conda list
- 搜索包
conda search package_name
2.2 使用virtualenv管理虛擬環(huán)境
pip install virtualenv
virtualenv --no-site-packages xb_test1
2.3 使用venv管理虛擬環(huán)境
# venv 不用安裝,需要python3.3以上
python -m venv ./xb_test2

3. PyTorch深度學(xué)習(xí)環(huán)境配置
3.1 Linux篇
前提是Anaconda各種環(huán)境都配置好
- 安裝CUDA (是NVIDIA推出的用于自家GPU的并行計算框架)
- 安裝PyTorch
- 在官網(wǎng)
https://pytorch.org/根據(jù)自己的系統(tǒng)、包管理器、語言以及CUDA版本獲取安裝命令,如conda install pytorch torchvision cudatoolkit=10.2 -c pytorch
- 在官網(wǎng)
- 安裝CUDNN加速((CUDA Deep Neural Network library),是NVIDIA打造的針對深度神經(jīng)網(wǎng)絡(luò)的加速庫,不必須)
# 卸載已有的舊版本
sudo apt-get purge cuda
sudo apt-get purge libcudnn6
sudo apt-get purge libcudnn6-dev
# 根據(jù)自己的系統(tǒng)版本下載
wget http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/cuda-repo-ubuntu1604_9.0.176-1_amd64.deb
wget http://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1604/x86_64/libcudnn7_7.0.5.15-1+cuda9.0_amd64.deb
wget http://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1604/x86_64/libcudnn7-dev_7.0.5.15-1+cuda9.0_amd64.deb
wget http://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1604/x86_64/libnccl2_2.1.4-1+cuda9.0_amd64.deb
wget http://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1604/x86_64/libnccl-dev_2.1.4-1+cuda9.0_amd64.deb
sudo dpkg -i cuda-repo-ubuntu1604_9.0.176-1_amd64.deb
sudo dpkg -i libcudnn7_7.0.5.15-1+cuda9.0_amd64.deb
sudo dpkg -i libnccl2_2.1.4-1+cuda9.0_amd64.deb
sudo dpkg -i libnccl-dev_2.1.4-1+cuda9.0_amd64.deb
sudo apt-get update
sudo apt-get install cuda=9.0.176-1
sudo apt-get install libcudnn7-dev
sudo apt-get install libnccl-dev
# 重啟系統(tǒng)
# 修改環(huán)境變量
export PATH=/usr/local/cuda-9.0/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda-9.0/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
3.2 Windows篇
https://app.yinxiang.com/fx/25e4c22d-f868-486e-9208-e77920e287c7