胖蔡说技术
随便扯扯

Python中使用国内源头下载依赖

Python开发中对于项目的依赖通常是用pip install命令进行下载,默认官网下载源为国外网络,会经常出现下载超时的问题。那么如何修改下载源使得我们能快速下载资源,如下介绍两种方式设置国内下载源。

国内下载源

首先来了解下国内python下载源地址:

https://pypi.tuna.tsinghua.edu.cn/simple/   # 清华
http://pypi.doubanio.com/simple/  # 豆瓣
http://mirrors.aliyun.com/pypi/simple/  # 阿里云
http://pypi.hustunique.com/  # 华中理工大学
http://pypi.sdutlinux.org/ # 山东理工大学
https://pypi.mirrors.ustc.edu.cn/simple/ # 中国科学技术大学
http://mirrors.163.com/pypi/simple/ # 网易

临时修改下载源

如果不想永久性修改下载源,pip提供了一个参数来设置当前下载源地址。如下,通过设置下载源来下载matplotlib依赖:

PS G:\projects\python\pythonNotes> pip install matplotlib -i https://pypi.tuna.tsinghua.edu.cn/simple
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting matplotlib
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/df/3f/6093a23565d0f50ce433f56223fcc34af6c912cd4331dc582ba29d9b5a17/matplotlib-3.5.3-cp37-cp37m-win_amd64.whl (7.2 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 7.2/7.2 MB 559.4 kB/s eta 0:00:00
Collecting pyparsing>=2.2.1
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/6c/10/a7d0fa5baea8fe7b50f448ab742f26f52b80bfca85ac2be9d35cdd9a3246/pyparsing-3.0.9-py3-none-any.whl (98 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 98.3/98.3 kB 434.0 kB/s eta 0:00:00
Collecting python-dateutil>=2.7
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/36/7a/87837f39d0296e723bb9b62bbb257d0355c7f6128853c78955f57342a56d/python_dateutil-2.8.2-py2.py3-none-any.whl (247 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 247.7/247.7 kB 379.9 kB/s eta 0:00:00
Collecting kiwisolver>=1.0.1
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/03/93/11790e8e81b89acd3a1c8a6b501f8a05b1c41beee0990582699cdda29557/kiwisolver-1.4.4-cp37-cp37m-win_amd64.whl (54 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 54.9/54.9 kB 357.2 kB/s eta 0:00:00
Collecting cycler>=0.10
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/5c/f9/695d6bedebd747e5eb0fe8fad57b72fdf25411273a39791cde838d5a8f51/cycler-0.11.0-py3-none-any.whl (6.4 kB)
Collecting numpy>=1.17
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/97/9f/da37cc4a188a1d5d203d65ab28d6504e17594b5342e0c1dc5610ee6f4535/numpy-1.21.6-cp37-cp37m-win_amd64.whl (14.0 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 14.0/14.0 MB 396.9 kB/s eta 0:00:00
Collecting packaging>=20.0
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/05/8e/8de486cbd03baba4deef4142bd643a3e7bbe954a784dc1bb17142572d127/packaging-21.3-py3-none-any.whl (40 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 40.8/40.8 kB 195.6 kB/s eta 0:00:00
Collecting fonttools>=4.22.0
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/e3/d9/e9bae85e84737e76ebbcbea13607236da0c0699baed0ae4f1151b728a608/fonttools-4.38.0-py3-none-any.whl (965 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 965.4/965.4 kB 531.5 kB/s eta 0:00:00
Collecting pillow>=6.2.0
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/d5/31/b026f9f7c87adf8027f51f98f392f6558982485b7202af5f9276492b2141/Pillow-9.3.0-1-cp37-cp37m-win_amd64.whl (2.5 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2.5/2.5 MB 476.6 kB/s eta 0:00:00
Collecting typing-extensions
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/0b/8e/f1a0a5a76cfef77e1eb6004cb49e5f8d72634da638420b9ea492ce8305e8/typing_extensions-4.4.0-py3-none-any.whl (26 kB)
Collecting six>=1.5
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/d9/5a/e7c31adbe875f2abbb91bd84cf2dc52d792b5a01506781dbcf25c91daf11/six-1.16.0-py2.py3-none-any.whl (11 kB)
Installing collected packages: typing-extensions, six, pyparsing, pillow, numpy, fonttools, cycler, python-dateutil, packaging, kiwisolver, matplotlib
Successfully installed cycler-0.11.0 fonttools-4.38.0 kiwisolver-1.4.4 matplotlib-3.5.3 numpy-1.21.6 packaging-21.3 pillow-9.3.0 pyparsing-3.0.9 python-dateutil-2.8.2 six-1.16.0 typing-extensions-4.4.0

永久性修改下载源

临时性指定下载源比较麻烦,我们可以通过设置pip配置信息来修改永久下载源。

1、Windows修改pip下载源

windows系统环境,在应用存储数据目录:

C:\Users\Administrator\AppData\Roaming

创建pip/pip.ini文件,添加设置如下:

[global]
timeout =  6000
index-url = https://pypi.tuna.tsinghua.edu.cn/simple/
trusted-host = pypi.tuna.tsinghua.edu.cn

重启启动终端,使用pip进行安装numpy

PS G:\projects\python\pythonNotes> pip install numpy
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple/
Requirement already satisfied: numpy in c:\users\administrator\appdata\local\programs\python\python37\lib\site-packages (1.21.6)
PS G:\projects\python\pythonNotes> pip install virtualenv
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple/
Collecting virtualenv
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/fd/76/c99d37939e17e3c53f1c1b7e4f5365a9160b1cd0b37700657eed9a2a6775/virtualenv-20.16.7-py3-none-any.whl (8.8 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 8.8/8.8 MB 373.4 kB/s eta 0:00:00
Collecting distlib<1,>=0.3.6
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/76/cb/6bbd2b10170ed991cf64e8c8b85e01f2fb38f95d1bc77617569e0b0b26ac/distlib-0.3.6-py2.py3-none-any.whl (468 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 468.5/468.5 kB 697.6 kB/s eta 0:00:00
Collecting filelock<4,>=3.4.1
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/94/b3/ff2845971788613e646e667043fdb5f128e2e540aefa09a3c55be8290d6d/filelock-3.8.0-py3-none-any.whl (10 kB)
Collecting platformdirs<3,>=2.4
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/61/e0/15ba41c6716acb033c3793be3a02f26c53914ecd9bdd6b315001f8f5f581/platformdirs-2.5.4-py3-none-any.whl (14 kB)
Collecting importlib-metadata>=4.8.3
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/e1/16/1f59f5d87d256012e9cdf0e8af8810965fa253e835cfecce64f4b11d4f2d/importlib_metadata-5.1.0-py3-none-any.whl (21 kB)
Collecting zipp>=0.5
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/d8/20/256eb3f3f437c575fb1a2efdce5e801a5ce3162ea8117da96c43e6ee97d8/zipp-3.11.0-py3-none-any.whl (6.6 kB)
Requirement already satisfied: typing-extensions>=3.6.4 in c:\users\administrator\appdata\local\programs\python\python37\lib\site-packages (from importlib-metadata>=4.8.3->virtualenv) (4.4.0)
Installing collected packages: distlib, zipp, platformdirs, filelock, importlib-metadata, virtualenv
Successfully installed distlib-0.3.6 filelock-3.8.0 importlib-metadata-5.1.0 platformdirs-2.5.4 virtualenv-20.16.7 zipp-3.11.0

通过上方返回信息可以看到下载源已经更新了~

2、Linux修改下载源

linux可以通过在home目录下创建 ~/.pip/pip.conf文件,输入配置如下:

[global]
timeout = 6000
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
trusted-host = pypi.tuna.tsinghua.edu.cn
赞(1) 打赏
转载请附上原文出处链接:胖蔡叨叨叨 » Python中使用国内源头下载依赖
分享到: 更多 (0)

请小编喝杯咖啡~

支付宝扫一扫打赏

微信扫一扫打赏