pip国内镜像源及配置方法


一、pip镜像源

以下为国内常用的PyPI镜像源,建议根据地理位置选择最近的镜像站:

镜像源名称地址特点与推荐使用场景
清华大学https://pypi.tuna.tsinghua.edu.cn/simple/同步频率高,国内高校首选
阿里云https://mirrors.aliyun.com/pypi/simple/稳定性好,覆盖全国多线路
中国科学技术大学https://pypi.mirrors.ustc.edu.cn/simple教育网优化,华中地区推荐
豆瓣http://pypi.douban.com/simple/老牌镜像源,历史悠久
腾讯云https://mirrors.cloud.tencent.com/pypi/simple华南地区用户推荐
华为云https://mirrors.huaweicloud.com/repository/pypi/simple/企业级稳定性,多地域覆盖
网易https://mirrors.163.com/pypi/simple/华东地区用户推荐
北京外国语大学https://mirrors.bfsu.edu.cn/pypi/web/simple/教育网用户优化

二、为何需要国内镜像源

Python的官方包索引(PyPI)服务器位于国外,国内用户直接访问时经常会遇到一些问题:

  • 下载速度缓慢,有时只有几十KB/s
  • 连接不稳定,经常中断或超时
  • 大型包(如TensorFlow、PyTorch)下载失败率高

使用国内镜像源可以显著提升下载速度,通常能达到10MB/s以上,大大提高了开发效率。这些镜像源会定期与PyPI官方源同步,确保包的及时更新和完整性。

三、安装时指定源

在安装特定包时,可以使用-i参数临时指定镜像源。

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

对于需要安装多个包的情况,可以一次性指定。

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ package1 package2 package3

四、使用信任源

某些镜像源可能使用自签名证书或证书配置不当,导致pip报SSL错误,此时可以使用--trusted-host参数。
这个参数告诉pip信任指定的主机名,即使其HTTPS证书有问题也继续连接。

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ --trusted-host pypi.tuna.tsinghua.edu.cn package_name

五、pip永久配置镜像源

1. 通过命令配置

最简单的方式是使用pip config命令。

# 设置默认镜像源
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple/

# 设置信任主机
pip config set global.trusted-host pypi.tuna.tsinghua.edu.cn

2. 手动修改配置文件

Linux/macOS

# 创建或编辑pip配置文件
mkdir -p ~/.pip
vim ~/.pip/pip.conf

Windows

# 在用户目录下创建 pip 文件夹和 pip.ini 文件
# 路径:%USERPROFILE%\pip\pip.ini

%USERPROFILE%通常指C:\Users\<你的用户名>目录

配置文件内容

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

# 可选:设置超时时间
timeout = 120

# 可选:禁用缓存(适用于测试环境)
# no-cache-dir = true

多镜像源备用配置
如果担心单一镜像源不稳定,可以配置多个备用源。

[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
    mirrors.aliyun.com
    pypi.mirrors.ustc.edu.cn

六、恢复默认配置

1. 删除配置文件

# Linux / macOS
rm ~/.pip/pip.conf

# Windows
del %USERPROFILE%\pip\pip.ini

2. 使用命令重置

pip config unset global.index-url
pip config unset global.trusted-host

七、镜像源选择

选择镜像源时,可以考虑以下因素:

  1. 地理位置:选择离你物理位置最近的镜像源

  2. 网络环境:

    • 教育网用户优先选择清华、中科大镜像
    • 商业网络用户可选择阿里云、腾讯云
  3. 同步频率:清华、阿里云通常同步频率较高

  4. 稳定性:大型云服务商提供的镜像通常更稳定

八、镜像源速度对比

可以使用以下命令测试镜像源速度(需要安装speedtest-cli)

# 安装speedtest-cli
pip install speedtest-cli

# 测试网络速度
speedtest-cli

九、相关文章

Conda国内镜像源及配置方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI手记叨叨

解锁“精神股东”称号!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值