需求
在开发时,部分包放置于内网但大部分包都是公网包,由于种种原因更新依赖容易超时,希望能够按需从不同的 pip 源上搜索不同的python package。
ERROR: Could not install packages due to an EnvironmentError: HTTPConnectionPool(host='internal-mirrors.xxx.xxx', port=8081): Max retries exceeded with url: /
repository/pypi-group/packages/wxpython/4.1.0/wxPython-4.1.0-cp38-cp38-win_amd64.whl (Caused by ReadTimeoutError("HTTPConnectionPool(host='internal-mirrors.xxx.xxx', port=8081): Read timed out. (read timeout=15)"))
方法
pip 支持在安装使用时需要指定多个源,让pip依次从多个源上搜索、下载、安装
- 命令中指定
pip install -i [pypi_source_1] --extra-index-url [pypi_source_2] --trusted-host [hostname of pypi_source_1] --trusted-host [hostname of pypi_source_2]
会优先从pypi_source_1上查找,如果找到则下载并安装,否则从pypi_source_2上查找,如果找到则下载并安装。
- 配置文件中指定
同样是~/.pip/pip.conf中配置,配制成如下内容
[global]
timeout=40
index-url= [pypi_source_1]
extra-index-url= [pypi_source_2]
[install]
trusted-host=
hostname of pypi_source_1
hostname of pypi_source_2
这个例子实现的效果与上面的命令行相同
细节
在配置时,优先把最稳定的 pip 源设置为 index-url:
- 如果内网稳定则设定内网为 index-url
- 如果外网稳定则设定外网为 index-url
附录
常用 pip 源:
http://mirrors.aliyun.com/pypi/simple/
https://pypi.tuna.tsinghua.edu.cn/simple/
http://pypi.mirrors.ustc.edu.cn/simple/