1. 首页
  2. 系统运维
  3. Linux

CentOS 7系统pip安装requests “def where() -> str”报错

随着CentOS 7即将寿终正寝,自带的python 2.7时代也将彻底随之落幕。这不还没到结束服务期间,问题就开始慢慢的出现。今天我在一台CentOS 7中用pip安装requests模块的时候就发现报错了。

和往常一样先用yum安装pip,然后pip install requests。但是实际从安装到导入都报错了。

安装报错

CentOS 7系统pip安装requests "def where() -> str"报错

首先最新版本的requests不支持python 2.7,需要安装2.28以下,所以我安装了2.27版本

========================== 
Unsupported Python version
========================== 
This version of Requests requires at least Python 3.7, but 
you're trying to install it on Python 2.7. To resolve this, 
consider upgrading to a supported Python version. 

If you can't upgrade your Python version, you'll need to 
pin to an older version of Requests (<2.28).

导入报错

CentOS 7系统pip安装requests "def where() -> str"报错
Python 2.7.5 (default, Jun 28 2022, 15:30:04) 
 [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux2
 Type "help", "copyright", "credits" or "license" for more information.
               import requests
       Traceback (most recent call last):
         File "", line 1, in 
         File "/usr/lib/python2.7/site-packages/requests/init.py", line 133, in 
           from . import utils
         File "/usr/lib/python2.7/site-packages/requests/utils.py", line 27, in 
           from . import certs
         File "/usr/lib/python2.7/site-packages/requests/certs.py", line 15, in 
           from certifi import where
         File "/usr/lib/python2.7/site-packages/certifi/init.py", line 1, in 
           from .core import contents, where
         File "/usr/lib/python2.7/site-packages/certifi/core.py", line 17
           def where() -> str:
                       ^
       SyntaxError: invalid syntax         

从这里可以看到,requests安装时候附带的模块也都已经不支持python 2.7了。如果你和我一样到这里,那么你想卸载都已经难了,因为pip都会报错。

CentOS 7系统pip安装requests "def where() -> str"报错

这个应该是CentOS自带的pip版本太老引起的,现在需要先卸载pip,然后重新安装新的pip。

更新PIP

wget https://bootstrap.pypa.io/pip/2.7/get-pip.py
python get-pip.py
pip install --upgrade setuptools

重新安装requests

更新完pip后,先彻底卸载requests和之前附带安装的所有模块,如果只是卸载requests再安装还是会报错。

pip uninstall certifi chardet idna requests urllib3 
CentOS 7系统pip安装requests "def where() -> str"报错
pip install requests
CentOS 7系统pip安装requests "def where() -> str"报错

CentOS 7下安装requests成功,导入成功。

联系我们

0574-55011290

QQ:248687950

邮件:admin@nbhao.org

工作时间:周一至周五,9:00-18:00,节假日休息

QR code