python-pip 的下载源镜像切换到国内的服务器

三种解决方案,按推荐程度排序:

方案一:永久配置(推荐,一劳永逸)

配置一次后,以后所有 pip install 命令都会自动使用国内镜像。

🍎 macOS / Linux

sh 复制代码
mkdir -p ~/.pip
cat << EOF > ~/.pip/pip.conf
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
trusted-host = pypi.tuna.tsinghua.edu.cn
EOF

如果是较新的 macOS 版本或某些 Linux 发行版,配置文件可能在 ~/.config/pip/pip.conf,如果上面的命令不生效,请尝试:

sh 复制代码
mkdir -p ~/.config/pip
cat << EOF > ~/.config/pip/pip.conf
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
trusted-host = pypi.tuna.tsinghua.edu.cn
EOF

Windows

方法 A (命令行): 打开 CMD 或 PowerShell,运行:
sh 复制代码
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
方法 B (手动创建):

在用户目录下创建文件夹:%APPDATA%\pip\ (通常在 C:\Users\你的用户名\AppData\Roaming\pip)。

在该文件夹下新建一个文本文件,命名为 pip.ini (注意后缀是 .ini 不是 .txt)。

用记事本打开,填入以下内容:

sh 复制代码
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
trusted-host = pypi.tuna.tsinghua.edu.cn

方案二:临时指定(仅对当前命令有效)

只想加速某一次安装,可以在命令后加上 -i 参数:

sh 复制代码
pip install <包名> -i https://pypi.tuna.tsinghua.edu.cn/simple

方案三:升级 pip 本身

有时候慢是因为 pip 版本太老。先升级 pip 也能提升解析速度:

sh 复制代码
# 使用国内源升级 pip
python -m pip install --upgrade pip -i https://pypi.tuna.tsinghua.edu.cn/simple

常用国内镜像源列表

如果清华源偶尔不可用,可以替换为以下任意一个:

表格 header
清华大学 (推荐) https://pypi.tuna.tsinghua.edu.cn/simple
阿里云 https://mirrors.aliyun.com/pypi/simple/
中国科学技术大学 https://pypi.mirrors.ustc.edu.cn/simple/
豆瓣 https://pypi.douban.com/simple/
华中科技大学 https://pypi.hustunique.com/simple/