相信大家在使用pip安装依赖包时很头疼,下载的网速超级慢,那是因为使用pip install命令时默认使用国外的源:https://files.pythonhosted.org,不妨ping一下,是美国的IP.

第1步:默认国外源:

1
2
3
4
5
6
7
8
9
10
11
如下命令:
(venv) iMac:20180921-demo raoys$ pip3 install --upgrade pip
Collecting pip
Downloading https://files.pythonhosted.org/packages/5f/25/
e52d3f31441505a5f3af41213346e5b6c221c9e086a166f3703d2ddaf940/pip-18.0-py2.py3-none-any.whl (1.3MB)
100% |████████████████████████████████| 1.3MB 556kB/s
Installing collected packages: pip
Found existing installation: pip 10.0.1
Uninstalling pip-10.0.1:
Successfully uninstalled pip-10.0.1
Successfully installed pip-18.0

下面教大家如何国内的源,体验飞的速度!!!

第2步:介绍有哪些国内源:

1
2
3
4
5
6
7
8
9
10
11
12
13
清华:https://pypi.tuna.tsinghua.edu.cn/simple

阿里云:http://mirrors.aliyun.com/pypi/simple/

中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/

华中理工大学:http://pypi.hustunique.com/

山东理工大学:http://pypi.sdutlinux.org/

豆瓣:http://pypi.douban.com/simple/

需要注意的是新版ubuntu要求使用https源。

第3步:开始使用国内源:

A、临时使用:可以在使用pip的时候加参数“-i https://pypi.tuna.tsinghua.edu.cn/simple”
例如:pip install -i https://pypi.tuna.tsinghua.edu.cn/simple requests,这样就会从清华这边的镜像去安装requests库。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

(venv) iMac:20180921-demo raoys$ pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple requests
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting requests
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/65/47
/7e02164a2a3db50ed6d8a6ab1d6d60b69c4c3fdf57a284257925dfc12bda/requests-2.19.1-py2.py3-none-any.whl (91kB)
100% |████████████████████████████████| 92kB 1.4MB/s
Collecting urllib3<1.24,>=1.21.1 (from requests)
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/bd/c9
/6fdd990019071a4a32a5e7cb78a1d92c53851ef4f56f62a3486e6a7d8ffb/urllib3-1.23-py2.py3-none-any.whl (133kB)
100% |████████████████████████████████| 143kB 399kB/s
Collecting chardet<3.1.0,>=3.0.2 (from requests)
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/bc/a9
/01ffebfb562e4274b6487b4bb1ddec7ca55ec7510b22e4c51f14098443b8/chardet-3.0.4-py2.py3-none-any.whl (133kB)
100% |████████████████████████████████| 143kB 2.1MB/s
Collecting idna<2.8,>=2.5 (from requests)
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/4b/2a
/0276479a4b3caeb8a8c1af2f8e4355746a97fab05a372e4a2c6a6b876165/idna-2.7-py2.py3-none-any.whl (58kB)
100% |████████████████████████████████| 61kB 2.2MB/s
Collecting certifi>=2017.4.17 (from requests)
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/df/f7
/04fee6ac349e915b82171f8e23cee63644d83663b34c539f7a09aed18f9e/certifi-2018.8.24-py2.py3-none-any.whl (147kB)
100% |████████████████████████████████| 153kB 1.9MB/s
Installing collected packages: urllib3, chardet, idna, certifi, requests
Successfully installed certifi-2018.8.24 chardet-3.0.4 idna-2.7 requests-2.19.1 urllib3-1.23
(venv) iMac:20180921-demo raoys$

B、永久修改,一劳永逸:

1
2
3
4
5
6
7
8
9
Linux: 修改 ~/.pip/pip.conf (没有就创建一个文件夹及文件。文件夹要加“.”,表示是隐藏文件夹)
MacOS: 修改 ~/.pip/pip.conf (没有就创建一个文件夹及文件。文件夹要加“.”,表示是隐藏文件夹)
windows:,直接在user目录中创建一个pip目录,再新建文件pip.ini。例如:C:\Users\jason\pip\pip.ini

内容如下:
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
[install]
trusted-host=mirrors.aliyun.com

C、配置后直接使用pip instal命令即可,无需加任何参数,如下命令:

1
2
3
4
5
6
7
8
9
10
11
12
13
(venv) iMac:20180921-demo raoys$ cd ~
(venv) iMac:~ raoys$ mkdir .pip
(venv) iMac:~ raoys$ cd .pip/
(venv) iMac:.pip raoys$ vi pip.conf
(venv) iMac:.pip raoys$ pip3 install beautifulsoup4
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting beautifulsoup4
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/21/0a
/47fdf541c97fd9b6a610cb5fd518175308a7cc60569962e776ac52420387/beautifulsoup4-4.6.3-py3-none-any.whl (90kB)
100% |████████████████████████████████| 92kB 1.3MB/s
Installing collected packages: beautifulsoup4
Successfully installed beautifulsoup4-4.6.3
(venv) iMac:.pip raoys$

以上就是pip使用国内源的所有步骤。