参考资料

  1. rocm-pypi安装加速
  2. riscv-toolchains安装加速
  3. puppy安装加速
  4. sury安装加速
  5. tex-historic-archive安装加速
  6. nginx安装加速
  7. scoop-extras.git安装加速
  8. raven安装加速

pypi-packages安装加速

PyPI 包安装加速完全指南

从 `pip install` 卡在 "Collecting" 到动辄几分钟的下载,PyPI 安装慢几乎是每个 Python 开发者都遇到过的痛点。本文系统梳理从换源、缓存、并发到离线镜像的加速方案,可按需组合使用。

一、先判断瓶颈在哪里

加速之前,先确认慢的原因,避免无效优化。 bash

查看 pip 实际使用的索引地址

pip config list pip config debug

带时间戳观察下载过程,定位是网络还是编译耗时

pip install -v numpy 常见三类瓶颈:

瓶颈类型典型表现主要对策
网络延迟/带宽下载 wheel 缓慢、超时换国内镜像源
无预编译包卡在 "Building wheel"使用二进制 wheel、预编译环境
依赖解析"Resolving dependencies" 长时间停滞新版解析器、约束文件、缓存

二、切换镜像源(最见效)

临时使用

bash pip install -i https://pypi.tuna.tsinghua.edu.cn/simple some-package

永久配置

bash pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple 等效于写入配置文件。各位置优先级:命令行 > 环境变量 > 用户配置 > 全局配置。

Linux/macOS 用户配置:`~/.config/pip/pip.conf` Windows:`%APPDATA%\pip\pip.ini` ini [global] index-url = https://pypi.tuna.tsinghua.edu.cn/simple extra-index-url = https://mirrors.aliyun.com/pypi/simple/ trusted-host = pypi.tuna.tsinghua.edu.cn mirrors.aliyun.com timeout = 60 常用镜像(请以各站最新说明为准):

  • 清华大学:`https://pypi.tuna.tsinghua.edu.cn/simple`
  • 阿里云:`https://mirrors.aliyun.com/pypi/simple/`
  • 腾讯云:`https://mirrors.cloud.tencent.com/pypi/simple/`
  • 中科大:`https://pypi.mirrors.ustc.edu.cn/simple/`

环境变量方式(适合 CI)

bash export PIP_INDEX_URL=https://pypi.tuna.tsinghua.edu.cn/simple export PIP_TRUSTED_HOST=pypi.tuna.tsinghua.edu.cn 注意 `extra-index-url` 存在依赖混淆风险:解析器可能从任一只源选取更高版本。企业环境建议使用私有源的单一入口。

三、启用并维护本地缓存

pip 默认开启 wheel 缓存,重复安装同一版本时无需重新下载。 bash

查看缓存目录

pip cache dir

查看缓存列表与占用

pip cache list pip cache info

清理(谨慎,会失去加速效果)

pip cache purge 缓存仅对相同 wheel 有效。若每次都重新编译源码包,缓存收益有限,需结合下文方案。

四、优先使用预编译 wheel

许多包(如 numpy、pandas、lxml)在源码安装时需要编译工具链,耗时远超下载本身。 bash

强制只使用二进制包,禁止源码回退

pip install --only-binary :all: pandas

查看某平台可用的 wheel

pip download --only-binary :all: --no-deps pandas -d ./whl 若目标平台无对应 wheel,可考虑:

  • 升级 pip 与 setuptools 以匹配新 wheel 标签
  • 使用 `manylinux` 兼容环境
  • 在目标平台构建后缓存为本地 wheel

五、构建本地/私有 wheel 仓库

团队或 CI 中,把依赖一次性下载,后续全部离线安装,效果最稳定。 bash

依据 requirements 下载全部 wheel 及其依赖

pip download -r requirements.txt -d ./wheels

离线安装

pip install --no-index --find-links ./wheels -r requirements.txt 进一步搭建简易 HTTP 服务供内网使用: bash python -m http.server 8080 --directory ./wheels

本地索引

pip install --index-url http://127.0.0.1:8080/simple --trusted-host 127.0.0.1 some-package 生产环境可选用 devpi、Nexus、Artifactory 等工具托管私有索引,兼顾速度与安全。

六、依赖解析与并发优化

bash

使用约束文件固定版本,减少回溯

pip install -r requirements.txt -c constraints.txt

提高超时与重试,适配不稳定网络

pip install --timeout 60 --retries 5 some-package

关闭版本检查(离线或内网)

pip install --disable-pip-version-check some-package `uv` 等新兴工具基于 Rust 实现,解析与下载速度通常显著优于传统 pip,可直接替换: bash uv pip install -r requirements.txt 是否采用需结合团队工具链评估。

七、代理、并发与系统层调整

bash

走代理

export HTTPS_PROXY=http://proxy.example.com:8080 export NO_PROXY=localhost,127.0.0.1

仅内网源时跳过证书校验(谨慎)

pip install --trusted-host mirrors.example.com some-package 镜像站通常不限制单连接带宽,但多进程并行安装仍可能触发限速,建议在 CI 中控制并发数,并遵守目标站点的访问策略,避免高频重试。

八、方案选择速查

场景推荐组合
本地开发国内镜像 + wheel 缓存
长期固定依赖镜像 + 约束文件 + 缓存
频繁重装环境本地 wheel 目录离线安装
CI 流水线私有源 / 预下载 wheels + `--no-index`
无网络环境全量 wheel 归档 + `--find-links`

九、常见问题

  • 换了源还是慢:确认配置生效(`pip config debug`),检查是否被 `--index-url` 命令行参数覆盖。
  • 一直 Building wheel:多为缺少预编译包,优先查找二进制 wheel 或补齐编译器。
  • SSL 证书报错:正确配置 `trusted-host`,而非全局关闭校验。
  • 内网完全离线:使用 `pip download` 预先归档,配合 `--no-index --find-links`。

小结

加速 PyPI 安装的核心逻辑是:减少网络往返、避免本地编译、复用已有产物。换源解决延迟,wheel 与缓存解决重复劳动,私有仓库与预下载解决稳定性。按实际瓶颈逐层组合,通常即可把安装时间从数分钟压缩到数秒级别。

作者:王壹杰
时间:2026-10-07
来源:https://mirror.ciilii.com/