Pip配置國(guó)內(nèi)鏡像源加速(2021.1.16)
[TOC]
Pip 是使用 Python 必不可少的包管理工具颂鸿,但是在使用默認(rèn)源下載包的時(shí)候,經(jīng)常出現(xiàn)因?yàn)榫W(wǎng)絡(luò)問(wèn)題導(dǎo)致超時(shí)的情況攒庵,因此有必要使用國(guó)內(nèi)鏡像源進(jìn)行下載加速嘴纺。
1. 國(guó)內(nèi)鏡像源
目前常用的鏡像源有清華源、阿里源等:
2. 演示環(huán)境
不同環(huán)境下相關(guān)命令可能需要進(jìn)行一些調(diào)整,本文編寫(xiě)時(shí)所處的環(huán)境如下:
- Ubuntu 20.04稳懒;
- Python 3.8.5闲擦;
- Pip 20.0.2(>=10.0.0);
3. 配置過(guò)程
關(guān)于 Pip 配置國(guó)內(nèi)鏡像源進(jìn)行加速的文章场梆,之前已經(jīng)有很多前輩寫(xiě)過(guò)了墅冷,基本都是手動(dòng)創(chuàng)建 Pip 的配置文件進(jìn)行修改。對(duì)于該方法有一點(diǎn)弊端或油,即在 Linux 與 Windows 環(huán)境下 Pip 配置文件的位置和名稱都有所不同寞忿,容易混淆,所以本文介紹一種通用的方法顶岸,想法來(lái)源于清華大學(xué)開(kāi)源軟件鏡像站 pypi 鏡像的使用幫助腔彰。
3.1 升級(jí) Pip 版本
因?yàn)樵摲椒ㄐ枰?Pip 版本大于等于 10.0.0叫编,所以需要先升級(jí) Pip 到 10.0.0 以上,如果 Pip已經(jīng)是最新版霹抛,此步可跳過(guò)搓逾;
# 升級(jí) Pip 版本
$ python3 -m pip install --upgrade pip
# 查看 Pip 版本
$ pip3 --version
3.2 配置國(guó)內(nèi)鏡像源
個(gè)人感覺(jué)阿里源更加穩(wěn)定,此處也以阿里源為例進(jìn)行演示杯拐;
# 配置 Pip 阿里鏡像源霞篡,--user參數(shù)表示當(dāng)前用戶生效
$ pip3 config set global.index-url https://mirrors.aliyun.com/pypi/simple --user
$ pip3 config set install.trusted-host mirrors.aliyun.com --user
3.3 查看是否成功
# 查看 Pip 所有配置項(xiàng)
$ pip3 config list --user
# 看到以下字段表示配置成功
global.index-url='https://mirrors.aliyun.com/pypi/simple/'
install.trusted-host='mirrors.aliyun.com'
3.4 其他操作
有關(guān) pip3 config
詳細(xì)的參數(shù)說(shuō)明可以通過(guò) pip3 config --help
命令查看其幫助文檔,下面只是列出一些可能會(huì)用到的操作:
# 查看配置項(xiàng)及配置文件路徑
$ pip3 config list -v
For variant 'global', will try loading '/etc/xdg/pip/pip.conf'
For variant 'user', will try loading '/home/xxx/.pip/pip.conf'
For variant 'user', will try loading '/home/xxx/.config/pip/pip.conf'
For variant 'site', will try loading '/usr/pip.conf'
global.index-url='https://mirrors.aliyun.com/pypi/simple/'
install.trusted-host='mirrors.aliyun.com'
# 在編輯器中打開(kāi)配置文件藕施,--editor參數(shù)指定使用的編輯器
$ pip3 config edit --editor vim --user
# 查看某項(xiàng)配置的值
$ pip3 config get global.index-url --user
https://mirrors.aliyun.com/pypi/simple
# 刪除某項(xiàng)配置
$ pip3 config unset global.index-url --user
Writing to /home/xxx/.config/pip/pip.conf