更换 PyPI 源的方法
国内用户访问 PyPI 官方源速度较慢,可通过更换为国内镜像源提升下载速度。常用镜像源包括清华、阿里云、腾讯云等。
临时使用镜像源安装包时,在 pip 命令后添加 -i 参数指定源地址:
pip install package_name -i https://pypi.tuna.tsinghua.edu.cn/simple
永久更换镜像源需修改 pip 配置文件。Linux/macOS 用户编辑 ~/.pip/pip.conf,Windows 用户编辑 %APPDATA%\pip\pip.ini,添加以下内容:
[global]
index-url = https://mirrors.aliyun.com/pypi/simple/
trusted-host = mirrors.aliyun.com
更换 CPython 源的方法
CPython 源码下载可通过更换镜像源加速。国内推荐使用华为云镜像:
git clone https://mirrors.huaweicloud.com/python/cpython.git
对于已存在的仓库,可通过修改 git 远程地址更换源:
git remote set-url origin https://mirrors.huaweicloud.com/python/cpython.git
常用镜像源列表
PyPI 镜像源:
- 清华:https://pypi.tuna.tsinghua.edu.cn/simple
- 阿里云:https://mirrors.aliyun.com/pypi/simple
- 腾讯云:https://mirrors.cloud.tencent.com/pypi/simple
CPython 镜像源:
- 华为云:https://mirrors.huaweicloud.com/python/cpython.git
- 清华:https://mirrors.tuna.tsinghua.edu.cn/git/python/cpython.git
验证源是否生效
安装测试包观察下载速度:
pip install numpy -i 选择的源地址
检查 CPython 克隆速度:
git clone --depth=1 选择的 CPython 源地址
注意事项
更换源后可能出现包版本不一致问题,重要生产环境建议测试后再部署。部分私有包可能不在镜像源中,需临时切换回官方源。
某些企业内网可能需要配置代理才能访问外部镜像源,此时需设置 HTTP_PROXY/HTTPS_PROXY 环境变量。

