pip install -r requirements.txt报错ReadTimeoutError: HTTPSConnectionPool
这个错误 ReadTimeoutError
是 网络连接超时 导致的,核心原因是网络无法稳定访问 PyPI 官方仓库(files.pythonhosted.org
是 Python 官方包仓库的文件存储地址),常见于网络不稳定、访问速度慢或被墙的情况。
解决方法:优先使用国内镜像源(最有效)
国内镜像源(如阿里云、清华等)同步了 PyPI 的所有内容,访问速度快,能避免超时问题。
方法 1:临时使用镜像源(单次有效)
安装时通过 -i
参数指定国内镜像源,例如使用阿里云镜像:
pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/
其他常用国内镜像源:
- 清华:
https://pypi.tuna.tsinghua.edu.cn/simple/
- 豆瓣:
https://pypi.doubanio.com/simple/
- 腾讯:
https://mirrors.cloud.tencent.com/pypi/simple/
方法 2:永久配置镜像源(一劳永逸)
将镜像源设置为默认,后续安装无需再手动指定:
-
创建/编辑 pip 配置文件:
# 对于 Linux/macOS,创建配置文件 mkdir -p ~/.pip && nano ~/.pip/pip.conf
-
添加镜像源配置:
在打开的文件中输入以下内容(以阿里云为例),保存退出(Ctrl+O
保存,Ctrl+X
退出):[global] index-url = https://mirrors.aliyun.com/pypi/simple/ [install] trusted-host = mirrors.aliyun.com
其他
科学上网法,需要花钱购代理(略)。