跳到主要内容

Pip

本页介绍临时或永久使用山河 PyPI 镜像安装 Python 软件包的方法。

修改配置前

先备份现有软件源配置,并确认当前环境能够解析和访问镜像站域名。系统级修改通常需要管理员权限。

一、检查域名解析

getent hosts mirrors.shanhe.com

如果当前网络没有配置相应 DNS,可在确认地址无误后添加本地解析:

echo '10.251.102.1 mirrors.shanhe.com' | sudo tee -a /etc/hosts

PyPI 镜像通常每 5 分钟同步一次,新发布的软件包可能会有短暂延迟。

二、临时使用

只对当前安装命令使用镜像:

pip install -i https://mirrors.shanhe.com/simple some-package
注意

地址末尾的 /simple 不能省略,并应使用 HTTPS。

三、设为默认镜像

先确认 Pip 版本:

python -m pip --version

Pip 10.0.0 及以上版本可以执行:

python -m pip install -i https://mirrors.shanhe.com/simple --upgrade pip
python -m pip config set global.index-url https://mirrors.shanhe.com/simple

四、验证配置

python -m pip config list
python -m pip install --dry-run requests

恢复默认配置

python -m pip config unset global.index-url