python报错没有request是什么原因

1,499次阅读
没有评论

python报错没有request是什么原因

requests介绍

requests是python的一个HTTP客户端库,跟urllib,urllib2类似,那我们为什么要用requests而不用urllib2呢?官方文档中是这样说明的:python的标准库urllib2提供了大部分需要的HTTP功能,但是API太逆天了,一个简单的功能就需要一大堆代码。所以requests是比较简单方便的库

问题

现在需要使用requests的脚本里引用了requests

import requests

运行时报错No module named ‘requests’。

使用命令

sudo python printBarcodeSex.py

完整报错如下:

[zzq@host252 script]$ sudo python printBarcodeSex.py 
[sudo] password for zzq: 
Traceback (most recent call last):
  File "printBarcodeSex.py", line 2, in <module>
    import requests
ImportError: No module named requests

原因

可能的原因是两种,一是没有安装requests的情况,但是还有一种情况是python环境的混乱,就是一台服务器上安装了很多个python的脚本,然后安装的requests只放置到了其中了一个版本的引用库中。

解决方式

首先确认是否安装有requests模块,使用安装命令:

sudo pip install requests

输出如下说明已经安装过了requests。

[zzq@host252 script]$ sudo pip install requests
Requirement already satisfied: requests in /usr/local/python27/lib/python2.7/site-packages
You are using pip version 9.0.3, however version 10.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:wuyou2019-10-05发表,共计977字。
新手QQ群:570568346,欢迎进群讨论 Python51学习
评论(没有评论)