Python电子邮件模块ImportError:没有名为utils的模块

joh*_*ual 11 python-2.7 python-requests

我的Python的请求库的安装已经好几个月了,它已经决定今天休息.

Python 2.7.6 (default, Jun 22 2015, 17:58:13) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  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 7, in <module>
    from .packages import chardet
  File "/usr/local/lib/python2.7/dist-packages/requests/packages/__init__.py", line 3, in <module>
    from . import urllib3
  File "/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/__init__.py", line 10, in <module>
    from .connectionpool import (
  File "/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/connectionpool.py", line 37, in <module>
    from .request import RequestMethods
  File "/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/request.py", line 6, in <module>
    from .filepost import encode_multipart_formdata
  File "/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/filepost.py", line 8, in <module>
    from .fields import RequestField
  File "/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/fields.py", line 1, in <module>
    import email.utils
ImportError: No module named utils
Run Code Online (Sandbox Code Playgroud)

调用麻烦utils与Python 2.7中的电子邮件模块有关

>>> import email
>>> import email.utils
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named utils
Run Code Online (Sandbox Code Playgroud)

utils显然是Python email库的一部分,但它无法访问.如果我卸载并重新安装:

$ sudo pip install email

Collecting email
  Downloading email-4.0.2.tar.gz (1.2MB)
    100% |????????????????????????????????| 1.2MB 286kB/s 
Installing collected packages: email
  Running setup.py install for email
Successfully installed email-4.0.2
Run Code Online (Sandbox Code Playgroud)

最糟糕的utils.py是实际存在.我究竟做错了什么?

$ ls /usr/local/lib/python2.7/dist-packages/email/
base64mime.py   errors.py       header.py      message.py      parser.pyc
base64mime.pyc  errors.pyc      header.pyc     message.pyc     quoprimime.py
charset.py      feedparser.py   __init__.py    mime            quoprimime.pyc
charset.pyc     feedparser.pyc  __init__.pyc   _parseaddr.py   test
encoders.py     generator.py    iterators.py   _parseaddr.pyc  utils.py
encoders.pyc    generator.pyc   iterators.pyc  parser.py       utils.pyc
Run Code Online (Sandbox Code Playgroud)

Luk*_*raf 19

实际上,很可能实际上调用了模块搜索路径中的一个模块(包括当前工作目录)email.这将导致Python取代该模块,它将email从标准库中隐藏模块,从而导致导入错误.

将该模块重命名为其他内容,您应该很好.