Python 下载加速:pip pypi 使用国内源及永久配置 pip 源

国内常见的 pip 源

来源 链接
阿里云 http://mirrors.aliyun.com/pypi/simple/
中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/
豆瓣 http://pypi.douban.com/simple/
清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/

临时使用方法

-i 加 url,举例:

1
pip install selenium -i http://pypi.douban.com/simple

如果使用的是 http 的源,会报如下错误(https 源不会有问题):

1
WARNING: The repository located at pypi.douban.com is not a trusted or secure host and is being ignored. If this repository is available via HTTPS we recommend you use HTTPS instead, otherwise you may silence this warning and allow it anyway with '--trusted-host pypi.douban.com'.

此时需要信任主机,使用 --trusted-host

1
pip install selenium -i http://pypi.douban.com/simple --trusted-host pypi.douban.com

如果有报错:

1
2
ERROR: Cannot unpack file C:\Users\issusers\AppData\Local\Temp\pip-unpack-6x9ra6uh\simple.html (downloaded from C:\Users\issusers\AppData\Local\Temp\pip-req-build-zoljdd4t, content-type: text/html); cannot detect archive format
ERROR: Cannot determine archive format of C:\Users\issusers\AppData\Local\Temp\pip-req-build-zoljdd4t

说明该源下可能没有同步到你需要的包,可以尝试更换源进行下载。

永久配置 pip 源

通过编辑配置文件

在当前用户根目录下,创建 .pip 目录,编辑配置文件

1
2
3
4
5
6
7
mkdir ~/.pip
tee ~/.pip/pip.conf <<-'EOF'
[global]
timeout=600
index-url=https://mirrors.aliyun.com/pypi/simple/
trusted-host=mirrors.aliyun.com
EOF

如果是 http 的源,配置文件最后还需要加上 trusted-host=mirrors.aliyun.com

如果使用的是 windows 系统,同样在用户的家目录下创建 .pip 文件夹,并写入 pip.conf 配置文件。

具体路径为 C:\Users\{你的系统用户名}\.pip\pip.conf

通过命令行配置 pip 源 <— 推荐,效率高

如果你不想编写配置文件,可以使用一条命令配置 pip 源,会自动生成以上目录及文件
升级 pip 到最新的版本 (>=10.0.0) 后进行配置:

1
2
3
4
# 升级 pip
pip install -i https://mirrors.aliyun.com/pypi/simple/ pip -U
# 生成 pip 配置文件
pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/