我是一名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'
到目前为止,我已经尝试了以下方法来解决这个问题,并没有修复它。:
我能想到的唯一可能导致这种情况的原因是我尝试使用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
我有什么想法可以解决这个问题吗?
谢谢!
答案 0 :(得分:1)
看起来您命名了一个自己的模块email.py
。
这将影响标准库模块email
,urllib3
尝试导入email.utils
,然后失败。
将你的模块重命名为其他东西,你应该很好。
注意:此指标为以下行:
File "email.py", line 1, in <module>
与回溯中的其他模块不同,此行不包含绝对路径,而是相对路径/只是文件名。这表明Python从当前工作目录而不是email.py
中选择了site-packages
模块。