前言
通过更换 pip 的源为国内镜像,可以大大加快包的下载速度。常用的国内镜像有阿里云、清华大学、豆瓣等。
1.临时更换源
如果我们只是偶尔需要更换源,可以在安装包时临时指定镜像源。例如:
pip install numpy -i https://pypi.tuna.tsinghua.edu.cn/simple
2.永久更换源
要永久更换 pip 的源,可以通过修改 pip 的配置文件来实现。
2.1 Windows系统
1.打开命令提示符,输入以下命令创建配置文件:
mkdir %HOMEPATH%\pip
notepad %HOMEPATH%\pip\pip.ini
2.在打开的记事本中输入以下内容,然后保存并关闭:
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
这将把 pip 的默认源更改为清华大学的镜像。
2.2 macOS 和 Linux 系统
1.在终端中输入以下命令创建配置文件:
mkdir ~/.pip
nano ~/.pip/pip.conf
2.在打开的 nano 编辑器中输入以下内容,然后保存并关闭(按 Ctrl + X,然后按 Y 确认):
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
可选的其他镜像源
阿里云:https://mirrors.aliyun.com/pypi/simple/
豆瓣:https://pypi.doubanio.com/simple/
中国科技大学:https://pypi.mirrors.ustc.edu.cn/simple/
华中科技大学:https://pypi.hustunique.com/