pip config set global.extra-index-url 用于添加备用镜像源,不替换主源,仅在主源404或超时后依次尝试;需用双引号包裹、空格分隔多个HTTPS地址,且各域名须单独配置trusted-host。

pip config set global.extra-index-url 添加补充源
直接用 pip config set global.extra-index-url 可以添加一个或多个备用镜像地址,它不会替换主源(index-url),而是作为“后备搜索列表”——pip 先查主源,找不到包时再依次尝试这些补充源。
常见错误是把多个 URL 写成一行但没加引号,或者用逗号分隔。正确写法必须用双引号包裹,URL 之间用空格分隔:
pip config set global.extra-index-url "https://mirrors.aliyun.com/pypi/simple/ https://pypi.mirrors.ustc.edu.cn/simple/"
注意:extra-index-url 不支持 HTTP 地址(如豆瓣的 http://pypi.douban.com/simple/),会因 SSL 验证失败报错 CertificateError;也不建议混用带 /web/simple 和 /simple/ 的路径,清华源最新推荐用 /simple/ 后缀,不是 /web/simple。
主源 + 补充源组合的实际效果
当你配置了 index-url 和 extra-index-url,pip 安装行为是:优先从主源下载包和元数据,若主源返回 404(包不存在)或网络超时,才轮询 extra-index-url 中的每个地址。它**不会并行请求,也不做负载均衡**。
这意味着:
- 主源失效时,安装会明显变慢(要等超时后才切到下一个)
- 如果某包只在阿里云源有、清华源没有,且你把阿里云设为
extra-index-url,它能装上;但如果阿里云排在第二个,而第一个补充源(比如 USTC)响应慢,就会卡住 -
pip search已被废弃,所以extra-index-url对搜索无影响,只影响install、download、wheel等操作
验证和排查多个源是否生效
运行 pip config list 能看到当前所有生效的配置项,包括 global.index-url 和 global.extra-index-url。但更关键的是观察实际安装日志:
生成 GitHub Actions、GitLab CI、Jenkins 的 CI/CD 流水线配置,适用于 Node.js、Python、Go、Docker 项目,支持回滚等配置。
加 -v 参数安装时,pip 会打印尝试的每一个 URL:
pip install requests -v 2>&1 | grep "Looking in indexes"
输出类似:
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple/, https://mirrors.aliyun.com/pypi/simple/, https://pypi.mirrors.ustc.edu.cn/simple/
如果只看到一个 URL,说明 extra-index-url 没写对格式,或被前面的配置覆盖了。多个 pip config set 命令会覆盖而非追加,所以想改多个源,必须一次性写全。
Windows 上 pip.ini 里手动写 multiple sources
命令行方式不灵时,可以退回到编辑配置文件。Windows 用户在 %APPDATA%\pip\pip.ini 中写:
[global] index-url = https://pypi.tuna.tsinghua.edu.cn/simple/ extra-index-url = https://mirrors.aliyun.com/pypi/simple/ https://pypi.mirrors.ustc.edu.cn/simple/ trusted-host = pypi.tuna.tsinghua.edu.cn trusted-host = mirrors.aliyun.com trusted-host = pypi.mirrors.ustc.edu.cn
关键点:
-
extra-index-url值必须写在同一行,空格分隔,不能换行 - 每个镜像域名都得单独加
trusted-host,否则可能触发 SSL 错误 - 如果
pip.ini存在语法错误(比如多了一个等号、缺中括号),pip 会静默忽略整个文件,导致你以为配好了其实没生效
最易被忽略的是:多个源之间没有“自动 fallback 超时控制”,超时时间由 global.timeout 统一控制,设太短会导致频繁跳过可用源,设太长又拖慢失败判断——建议保持默认 60 秒,不要盲目调低。

















