Requests.package.urllib3 错误

Requests.package.urllib3 error

我是一名 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'

到目前为止,我已经尝试了以下方法来解决这个问题,但没有任何解决办法。:

  1. 升级请求和urllib3
  2. 删除并重新安装 urllib3
  3. 重新启动 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

这将隐藏标准库模块 emailurllib3 尝试从中导入 email.utils,然后失败。

将您的模块重命名为其他名称,您应该会很好。


注意:这个指标是下面一行:

  File "email.py", line 1, in <module>

与回溯中的其他模块不同,这一行不包含绝对路径,而是一个相对路径/只是一个文件名。这表明 Python 从当前工作目录而不是 site-packages.

中选取了 email.py 模块