目录

存在问题

在采用默认 pip3 安装第三方库的时候,经常会出现超时的情况。
​​​pip._vendor.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out.​

这时候就需要替换镜像源为国内的镜像源了。

国内的pip源

  • 阿里云:https://mirrors.aliyun.com/pypi/simple/
  • 清华:https://pypi.tuna.tsinghua.edu.cn/simple
  • 中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/
  • 华中理工大学:http://pypi.hustunique.com/
  • 山东理工大学:http://pypi.sdutlinux.org/
  • 豆瓣:http://pypi.douban.com/simple/

临时更换镜像源

pip3 install 库名 -i 镜像地址

示例:

  • 安装 django-excel 库 并使用 阿里云 的镜像源
pip3 install django-excel -i https://mirrors.aliyun.com/pypi/simple/

查看执行是否替换镜像,如下:

pip pip3 替换国内镜像源_镜像源

可以看到镜像已经替换阿里云的镜像。

Windows下更换镜像源

打开我的电脑,在地址栏中输入 ​​%APPDATA%​​ 按回车跳转到目标目录。在目录下创建一个pip文件,再其内部创建一个pip.ini 文件。输入以下信息。以阿里云为例

[global]
timeout = 6000
index-url = https://mirrors.aliyun.com/pypi/simple/
trusted-host = mirrors.aliyun.com

注意:不管你用的是pip3还是pip,方法都是一样的,都是创建pip文件夹。

pip pip3 替换国内镜像源_镜像源_02

再次执行下载,查看下载的地址是否更换,如下:

pip pip3 替换国内镜像源_镜像源_03

可以看到下载镜像源已经变更为阿里云镜像了。

Linux 或者Mac环境替换镜像源:

  • 创建配置文件​​~/.pip/pip.conf​
[root@server01 ~]# mkdir -p ~/.pip
[root@server01 ~]# touch ~/.pip/pip.conf
  • 在​​pip.conf​​配置镜像源
[global]
timeout = 6000
index-url = https://mirrors.aliyun.com/pypi/simple/
trusted-host = mirrors.aliyun.com
  • 执行安装,查看镜像源是否替换

pip pip3 替换国内镜像源_镜像源_04

pip pip3 替换国内镜像源_django_05