My installation of Python's requests library has been working perfectly for months and it has decided to break today.
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
The trouble calling utils
is related to the email module in Python 2.7
>>> import email
>>> import email.utils
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named utils
utils
is clearly a part of Python's email
library, but it can't be accessed. If I uninstall and reinstall:
$ 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
Worst of all utils.py
is actually present. What am I doing wrong?
$ 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
To get rid of this error “ImportError: No module named”, you just need to create __init__.py in the appropriate directory and everything will work fine.
Python's ImportError ( ModuleNotFoundError ) indicates that you tried to import a module that Python doesn't find. It can usually be eliminated by adding a file named __init__.py to the directory and then adding this directory to $PYTHONPATH .
Most likely one of your own modules in the module search path (including the current working directory) is actually called email
. This will cause Python to pick up that module instead, and it will shadow the email
module from the standard library, leading to that import error.
Rename that module to something else, and you should be good.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With