手把手教你:在 Windows 系统配置 Python 国内镜像源

解决 pip 安装缓慢的终极方案,速度提升 10 倍+

为什么需要镜像源?

当使用 pip install 安装 Python 包时,默认连接国外服务器,常因网络延迟导致:

  • 下载速度缓慢(几十KB/s)
  • 频繁出现超时错误
  • 大型库安装失败

国内镜像源通过部署国内服务器,可显著提升下载速度至 5-10MB/s


配置方法(两种方案任选)

✅ 方案一:永久配置(推荐)
  1. 创建配置文件

    # 打开 PowerShell 执行
    New-Item -Path "$env:APPDATA\pip" -ItemType Directory -Force
    New-Item -Path "$env:APPDATA\pip\pip.ini" -Force
    

  2. 编辑配置文件
    用记事本打开 C:\Users\[你的用户名]\AppData\Roaming\pip\pip.ini,输入:

    [global]
    index-url = https://pypi.tuna.tsinghua.edu.cn/simple
    trusted-host = pypi.tuna.tsinghua.edu.cn
    

    常用镜像源(任选其一):

    • 清华:https://pypi.tuna.tsinghua.edu.cn/simple
    • 阿里:https://mirrors.aliyun.com/pypi/simple/
    • 腾讯:https://mirrors.cloud.tencent.com/pypi/simple
✅ 方案二:临时使用(单次生效)

安装包时添加 -i 参数:

pip install numpy -i https://pypi.tuna.tsinghua.edu.cn/simple


验证配置是否成功

# 查看当前源配置
pip config list

成功时显示:

global.index-url='https://pypi.tuna.tsinghua.edu.cn/simple'


实测速度对比

操作 默认源 国内镜像源
安装 numpy 3分12秒 9秒
下载 pandas 断连3次 一次成功

常见问题解决

  1. SSL证书错误
    在配置文件中增加:

    [global]
    trusted-host = pypi.tuna.tsinghua.edu.cn
    

  2. 权限不足
    以管理员身份运行 PowerShell 或 CMD

  3. 配置文件路径错误
    检查路径是否为:
    C:\Users\[用户名]\AppData\Roaming\pip\pip.ini


终极提示:配置完成后,建议运行 pip cache purge 清除旧缓存,享受飞一般的安装体验! 🚀

Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐