2

I have a Python script that uses requests. It worked fine for a long time. Now out of the blue I get the following error. I tried reinstalling requests but that didn't fix it. The only thing I can think of that caused the error is I have been running a Django development server, so maybe I got hacked? Please help.

code:

import requests
...

error:

Traceback (most recent call last):
File "myfile.py", line 1, in <module>
import requests
File "/Users/myuser/.virtualenvs/mysite/lib/python2.7/site-packages/requests/__init__.py", line 58, in <module>
from . import utils
File "/Users/myuser/.virtualenvs/mysite/lib/python2.7/site-packages/requests/utils.py", line 12, in <module>
import cgi
File "/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python2.7/cgi.py", line 50, in <module>
import mimetools
File "/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python2.7/mimetools.py", line 6, in <module>
import tempfile
File     "/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python2.7/tempfile.py", line 35, in <module>
from random import Random as _Random
ImportError: cannot import name Random
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
user2104778
  • 992
  • 1
  • 14
  • 38

1 Answers1

10

You have masked the built-in library module with a local file named random.py. Rename that file.

Python looks up modules to import along a path, and if you put a module in a location that's looked at before the standard library, you can end up masking a built-in like you did here.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343