文章目录
一、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
七、镜像源选择
选择镜像源时,可以考虑以下因素:
-
地理位置:选择离你物理位置最近的镜像源
-
网络环境:
- 教育网用户优先选择清华、中科大镜像
- 商业网络用户可选择阿里云、腾讯云
-
同步频率:清华、阿里云通常同步频率较高
-
稳定性:大型云服务商提供的镜像通常更稳定
八、镜像源速度对比
可以使用以下命令测试镜像源速度(需要安装speedtest-cli)
# 安装speedtest-cli
pip install speedtest-cli
# 测试网络速度
speedtest-cli