背景


在使用pip下载python模块的时候,默认是从python官方库下载,而在国内下载速度非常慢,且容易超时导致失败,一般情况下我们会在命令行中指定下载源,如下所示:

1
pip install -U httprunner -i http://pypi.douban.com/simple --trusted-host pypi.douban.com

但是这样不方便,难不成每一次都要我手动指定吗,能不能更改pip的默认下载地址?

解决


设为默认:
升级pip到最新的版本(>=10.0.0)后可配置,首先指定国内源更新pip版本:

1
python -m pip install --upgrade pip -i http://pypi.douban.com/simple --trusted-host pypi.douban.com

更改python默认下载地址:

1
pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/

国内常用的镜像源(随自己喜好,任意设置一个国内源即可):

1
2
3
https://pypi.tuna.tsinghua.edu.cn/simple/
https://mirrors.aliyun.com/pypi/simple/
http://pypi.douban.com/simple

按照上述设置后,安装python模块时直接使用pip install module即可:

1
2
3
4
5
6
7
PS D:\Blog> pip install httprunner
Looking in indexes: https://mirrors.aliyun.com/pypi/simple/
Collecting httprunner
Downloading https://mirrors.aliyun.com/pypi/packages/12/9e/1f373003a7dab32cf703912055a2da20170da71e6ede8e8600e430388236/httprunner-3.1.4-py3-none-any.whl (60 kB)
|████████████████████████████████| 60 kB 868 kB/s
Requirement already satisfied: requests<3.0.0,>=2.22.0 in c:\users\lzy\appdata\local\programs\python\python38\lib\site-packages (from httprunner) (2.24.0)
...