焦作市葬花

更换pip国内镜像源提升Python包下载速度的配置方法

2026-03-27 16:25:02 浏览次数:1
详细信息

一、临时使用镜像源

在安装包时直接指定镜像源:

pip install 包名 -i https://mirrors.aliyun.com/pypi/simple/

常用镜像源:

二、永久配置镜像源

方法1:使用 pip config 命令(推荐)

# 设置全局镜像源(对当前用户生效)
pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/

# 设置镜像源并信任
pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/
pip config set install.trusted-host mirrors.aliyun.com

方法2:手动编辑配置文件

Linux/macOS:

# 创建 pip 配置目录
mkdir -p ~/.pip

# 编辑配置文件
vim ~/.pip/pip.conf

# 添加以下内容:
[global]
index-url = https://mirrors.aliyun.com/pypi/simple/
trusted-host = mirrors.aliyun.com

Windows:

# 在用户目录下创建 pip 文件夹
# 路径:C:\Users\你的用户名\pip\pip.ini

# 添加以下内容:
[global]
index-url = https://mirrors.aliyun.com/pypi/simple/
trusted-host = mirrors.aliyun.com

三、验证配置

查看当前配置:

pip config list

测试下载速度:

pip install --upgrade pip

四、额外配置(可选)

1. 超时设置

pip config set global.timeout 60
pip config set global.default-timeout 100

2. 使用多个镜像源(备用)

编辑配置文件添加:

[global]
index-url = https://mirrors.aliyun.com/pypi/simple/
extra-index-url = 
    https://pypi.tuna.tsinghua.edu.cn/simple/
    https://pypi.org/simple
trusted-host = 
    mirrors.aliyun.com
    pypi.tuna.tsinghua.edu.cn

五、注意事项

HTTPS 问题:部分镜像源可能需要添加 trusted-host 参数 缓存清理:修改配置后可以清理缓存:pip cache purge 虚拟环境:在虚拟环境中需要单独配置,或者全局配置对所有环境生效 镜像同步:国内镜像有时会有延迟,如需最新包可临时使用官方源

选择离你地理位置较近的镜像源效果最佳,通常阿里云和清华源都是不错的选择。

相关推荐