Python中使用pip安装第三方库报错解决方法

对于刚开始学习Python的同学来说,经常在使用’‘pip install **’'进行安装库的时候会出现报错,像下面这样:

C:\Users\Golo>pip install flask
Collecting flask
  Downloading Flask-1.1.1-py2.py3-none-any.whl (94 kB)
     |██████████████                  | 40 kB 1.2 kB/s eta 0:00:47ERROR: Exception:
Traceback (most recent call last):
  File "d:\python\lib\site-packages\pip\_vendor\urllib3\response.py", line 425, in _error_catcher
    yield
  File "d:\python\lib\site-packages\pip\_vendor\urllib3\response.py", line 507, in read
    data = self._fp.read(amt) if not fp_closed else b""
  File "d:\python\lib\site-packages\pip\_vendor\cachecontrol\filewrapper.py", line 62, in read
    data = self.__fp.read(amt)
  File "d:\python\lib\http\client.py", line 447, in read      
   File "d:\python\lib\contextlib.py", line 130, in __exit__
    self.gen.throw(type, value, traceback)
  File "d:\python\lib\site-packages\pip\_vendor\urllib3\response.py", line 430, in _error_catcher
    raise ReadTimeoutError(self._pool, None, "Read timed out.")
pip._vendor.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out.

注意这里的报错Read timed out

接下来我们处理这个问题,首先再你的Python安装目录下创建一个pip.ini文件

Python中使用pip安装第三方库报错解决方法_第1张图片

然后 我们用记事本打开这个pip.ini文件,将下面内容复制进去,然后保存。

Python中使用pip安装第三方库报错解决方法_第2张图片

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

其中,index-url = https://pypi.tuna.tsinghua.edu.cn/simple/为国内镜像站,大家也可以使用下边这几个镜像站。
/// 阿里云 https://mirrors.aliyun.com/pypi/simple/
///中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/
///豆瓣(douban) https://pypi.douban.com/simple/
///清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/
///中国科学技术大学 https://pypi.mirrors.ustc.edu.cn/simple/*

pip.ini文件添加成功后,我们再进行安装,就会发现我们不会再报错了。

Successfully installed MarkupSafe-1.1.1 Werkzeug-1.0.0 flask-1.1.1已经成功

C:\Users\Golo>pip install flask
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple/
Collecting flask
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/9b/93/628509b8d5dc749656a9641f4caf13540e2cdec85276964ff8f43bbb1d3b/Flask-1.1.1-py2.py3-none-any.whl (94 kB)
     |████████████████████████████████| 94 kB 467 kB/s
Requirement already satisfied: Jinja2>=2.10.1 in d:\python\lib\site-packages (from flask) (2.11.0)
Requirement already satisfied: itsdangerous>=0.24 in d:\python\lib\site-packages (from flask) (1.1.0)
Collecting Werkzeug>=0.15
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/ba/a5/d6f8a6e71f15364d35678a4ec8a0186f980b3bd2545f40ad51dd26a87fb1/Werkzeug-1.0.0-py2.py3-none-any.whl (298 kB)
     |████████████████████████████████| 298 kB 1.3 MB/s
Requirement already satisfied: click>=5.1 in d:\python\lib\site-packages (from flask) (7.0)
Collecting MarkupSafe>=0.23
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/65/c6/2399700d236d1dd681af8aebff1725558cddfd6e43d7a5184a675f4711f5/MarkupSafe-1.1.1-cp37-cp37m-win_amd64.whl (16 kB)
Installing collected packages: Werkzeug, flask, MarkupSafe
Successfully installed MarkupSafe-1.1.1 Werkzeug-1.0.0 flask-1.1.1

你可能感兴趣的:(python入门)