前言
學習需要,在配置某個Python項目時需要使用特定版本(2.2.4)的matplotlib庫,於是就想使用Anaconda創建一個虛擬環境,再安裝這個包。在Anaconda Navigator的Environments中創建好了一個新的環境,結果在裏面搜索安裝matplotlib時發現conda中的版本沒有2.2.4的,2版本的最高是2.2.3。便想先安裝個2.2.3的試一試,結果安裝十分緩慢,各種網絡錯誤。無奈之下,只好選擇使用鏡像。
conda使用清華鏡像
首先打開Anaconda Prompt,切換到自己的虛擬環境。之後分別輸入以下三條命令執行:
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進行包的下載安裝時將會使用清華鏡像,速度會快很多。比如:
conda install matplotlib=2.2.3
然而,儘管這個庫下載成功了,項目中程序依舊報錯。看來必須要安裝2.2.4版本的纔可以。不過很奇怪,conda無法安裝2.2.4,報這樣的錯誤:
conda install matplotlib=2.2.4
Collecting package metadata: done
Solving environment: failed
PackagesNotFoundError: The following packages are not available from current channels:
- matplotlib=2.2.4
在同學建議下轉而使用pip
pip使用清華鏡像
1.當次使用
在使用pip install命令時可以在後面加上參數-i https://pypi.tuna.tsinghua.edu.cn/simple,這樣在本次安裝庫時將會選擇使用清華鏡像來下載,比如
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple matplotlib==2.2.4
但這樣只是這一次生效,如果每次都這樣的話將會比較麻煩。
2.永久使用
在Windows環境下,可以直接在當前用戶的目錄(比如我的目錄就是C:\User\Glamour)下新建一個pip目錄,在裏面新建一個pip.ini文件,寫入如下內容:
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
[install]
trusted-host=mirrors.aliyun.com
保存即可。之後使用pip安裝包時就快多了。
而且奇怪的是在Anaconda Navigator中看不到已經用pip安裝的包:
而使用conda list命令則是可以看到所有的包:
(Draw2) C:\Users\Glamour>conda list
# packages in environment at C:\Users\Glamour\Anaconda3\envs\Draw2:
#
# Name Version Build Channel
ca-certificates 2019.1.23 0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
certifi 2019.3.9 py37_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
cycler 0.10.0 pypi_0 pypi
decorator 4.4.0 pypi_0 pypi
fa2 0.3.5 pypi_0 pypi
kiwisolver 1.0.1 pypi_0 pypi
matplotlib 2.2.4 pypi_0 pypi
networkx 2.2 pypi_0 pypi
numpy 1.16.2 pypi_0 pypi
openssl 1.1.1b he774522_1 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
pip 19.0.3 py37_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
pyparsing 2.3.1 pypi_0 pypi
python 3.7.2 h8c8aaf0_10 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
python-dateutil 2.8.0 pypi_0 pypi
pytz 2018.9 pypi_0 pypi
scipy 1.2.1 pypi_0 pypi
setuptools 40.8.0 py37_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
six 1.12.0 pypi_0 pypi
sqlite 3.27.2 he774522_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
tqdm 4.31.1 pypi_0 pypi
vc 14.1 h0510ff6_4 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
vs2015_runtime 14.15.26706 h3a45250_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
wheel 0.33.1 py37_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
wincertstore 0.2 py37_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
參考
- Anaconda 鏡像使用幫助,https://mirrors.tuna.tsinghua.edu.cn/help/anaconda/
- 讓python pip使用國內鏡像,https://www.cnblogs.com/wqpkita/p/7248525.html