Jay L
Jay L

Reputation: 395

Requests.package.urllib3 error

I am a Python rookie and have been exploring the language. Things are going well since I do have a fairly basic programming background and I can understand the general structure. However, recently, I think that I did something to break urllib3 because my app was working before and now it fails and I can no longer import urllib3 through the interactive interpreter.

Here is the code:

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

This pretty basic stuff and it worked perfectly until earlier this morning. The error I now receive is the following:

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'

So far, I have tried the following to address the situation and nothing has fixed it.:

  1. Upgrade requests and urllib3
  2. Remove and reinstall urllib3
  3. Reboot Raspberry Pi <- Probably a dumb idea, but I tried anyway

The only thing that I can think of that could be causing this was that I tried to use pip to install stmtplib and email.utils and both failed.

One final data point is that if I try to import urllib3 through the interactive interpreter, I get the following:

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

Any ideas how I can fix this?

Thank you!

Upvotes: 2

Views: 4002

Answers (1)

Lukas Graf
Lukas Graf

Reputation: 32570

It looks like you named one of your own modules email.py.

This will shadow the standard library module email, from which urllib3 tries to import email.utils, and then fails.

Rename your module to something else, and you should be good.


Note: The indicator for this is the following line:

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

Unlike the other modules in the traceback, this line does not include an absolute path, but is instead a relative path / just a filename. This indicates that Python picked up the email.py module from the current working directory instead of its site-packages.

Upvotes: 1

Related Questions