pip 默认访问国外的镜像源来安装 Python 包会很慢且困难,为了提高下载速度,可以指定国内的镜像源。
在安装包时,可以使用 -i 参数指定临时镜像源。例如,使用清华大学的镜像源安装 django 包:
pip install django -i https://pypi.tuna.tsinghua.edu.cn/simple
永久指定 pip 镜像源的操作方法:
在 Linux 或 macOS 系统中:
创建或修改 ~/.pip/pip.conf 文件(如果不存在该目录,可以先运行 mkdir ~/.pip)内容如下:
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
[install]
trusted-host = pypi.tuna.tsinghua.edu.cn
在 Windows 系统中:
创建或修改 %HOMEPATH%\pip\pip.ini 文件,在该文件中添加以下内容:
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
[install]
trusted-host = pypi.tuna.tsinghua.edu.cn
以下是一些常用的国内镜像源地址:
清华大学:https://pypi.tuna.tsinghua.edu.cn/simple
阿里云:http://mirrors.aliyun.com/pypi/simple/
中国科技大学:https://pypi.mirrors.ustc.edu.cn/simple/