2

我是一名 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. 重启树莓派 <- 可能是个愚蠢的想法,但我还是试过了

我能想到的唯一可能导致这种情况的是我尝试使用 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

有什么想法可以解决这个问题吗?

谢谢!

4

1 回答 1

1

看起来您命名了您自己的模块之一email.py

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

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


注意:此指标为以下行:

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

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

于 2015-04-06T15:27:19.757 回答