背景:如果不配置国内镜像源(如清华、阿里云等),大陆地区使用默认的官方 PyPI 源pypi.org/simple 下载Python 包时,无法下载或者超时。
检查当前配置
使用 pip config list -v 查看当前配置。注意,实际优先级site>user>global。
(base) onlycode@onlycode:~$ pip config list -v
For variant 'global', will try loading '/etc/xdg/pip/pip.conf'
For variant 'global', will try loading '/etc/pip.conf'
For variant 'user', will try loading '/home/onlycode/.pip/pip.conf'
For variant 'user', will try loading '/home/onlycode/.config/pip/pip.conf'
For variant 'site', will try loading '/home/onlycode/miniforge3/pip.conf'
更新pip.inf文件
这里选择在'user'级的目录下创建pip.ini 文件
mkdir -p /home/onlycode/.pip/ && vim /home/onlycode/.pip/pip.conf
输入以下内容
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
extra-index-url = https://mirrors.aliyun.com/pypi/simple
https://pypi.org/simple/
[install]
trusted-host = pypi.tuna.tsinghua.edu.cn
mirrors.aliyun.com
pypi.org
验证配置
使用 pip config list -v 查看更新后配置。然后去下载验证即可。
(base) onlycode@onlycode:~$ pip config list -v
For variant 'global', will try loading '/etc/xdg/pip/pip.conf'
For variant 'global', will try loading '/etc/pip.conf'
For variant 'user', will try loading '/home/onlycode/.pip/pip.conf'
For variant 'user', will try loading '/home/onlycode/.config/pip/pip.conf'
For variant 'site', will try loading '/home/onlycode/miniforge3/pip.conf'
global.extra-index-url='https://mirrors.aliyun.com/pypi/simple\nhttps://pypi.org/simple/'
global.index-url='https://pypi.tuna.tsinghua.edu.cn/simple'
install.trusted-host='pypi.tuna.tsinghua.edu.cn\nmirrors.aliyun.com\npypi.org'
其他
miniconda 换源:Anaconda Mirror | SUSTech Open Source Mirrors