我是Python新手,并且一直在探索这种语言。事情进展顺利,因为我确实具有相当基本的编程背景,并且我可以理解总体结构。但是,最近,我认为我做了一些中断urllib3的操作,因为我的应用程序之前一直在工作,现在它失败了,并且我无法再通过交互式解释器导入urllib3。
这是代码:
import json, requests, ssl
devID = "xxxx" <--xxx is included for privacy/security purposes
varName = "Current_T"
AToken = "xxx" <--xxx is included for privacy/security purposes
spark_url = "https://api.spark.io/v1/devices/%s/%s?access_token=%s" % (devID,varName,AToken)
r = requests.get(spark_url)
data = r.json()
jsonData = "result"
CurrentTemp = data[jsonData]
print("Current temp is: %i") % CurrentTemp
这个非常基本的东西,直到今天早上一直运作良好。我现在收到的错误如下:
pi@raspberrypi ~/python-learning $ python ./spark-read-variable.py
Traceback (most recent call last):
File "./spark-read-variable.py", line 1, in <module>
import json, requests, ssl
File "/usr/local/lib/python2.7/dist-packages/requests/__init__.py", line 58, in <module>
from . import utils
File "/usr/local/lib/python2.7/dist-packages/requests/utils.py", line 26, in <module>
from .compat import parse_http_list as _parse_list_header
File "/usr/local/lib/python2.7/dist-packages/requests/compat.py", line 42, in <module>
from .packages.urllib3.packages.ordered_dict import OrderedDict
File "/usr/local/lib/python2.7/dist-packages/requests/packages/__init__.py", line 95, in load_module
raise ImportError("No module named '%s'" % (name,))
ImportError: No module named 'requests.packages.urllib3'
到目前为止,我已经尝试了以下方法来解决这种情况,但没有任何解决方法。
升级请求和urllib3
删除并重新安装urllib3
重新启动Raspberry Pi
我唯一能想到的可能是导致此问题的原因是我尝试使用pip安装stmtplib和email.utils,但均失败了。
最后一个数据点是,如果我尝试通过交互式解释器导入urllib3,则会得到以下信息:
Python 2.7.3 (default, Mar 18 2014, 05:13:23)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import urllib3
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/urllib3/__init__.py", line 10, in <module>
from .connectionpool import (
File "/usr/local/lib/python2.7/dist-packages/urllib3/connectionpool.py", line 37, in <module>
from .request import RequestMethods
File "/usr/local/lib/python2.7/dist-packages/urllib3/request.py", line 6, in <module>
from .filepost import encode_multipart_formdata
File "/usr/local/lib/python2.7/dist-packages/urllib3/filepost.py", line 8, in <module>
from .fields import RequestField
File "/usr/local/lib/python2.7/dist-packages/urllib3/fields.py", line 1, in <module>
import email.utils
File "email.py", line 1, in <module>
import smtplib
File "/usr/lib/python2.7/smtplib.py", line 46, in <module>
import email.utils
ImportError: No module named utils
有什么想法我可以解决这个问题吗?
谢谢!
最佳答案
您似乎已将自己的模块之一命名为email.py
。
这将遮盖标准库模块email
,urllib3
将从中尝试导入email.utils
,然后失败。
将模块重命名为其他名称,您应该会很好。
注意:指示符为以下行:
File "email.py", line 1, in <module>
与回溯中的其他模块不同,此行不包含绝对路径,而是相对路径/只是文件名。这表明Python从当前工作目录而不是其
email.py
中拾取了site-packages
模块。关于python - Requests.package.urllib3错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29474143/