导入一个模块(请求),得到错误“无法导入名称随机”

时间:2014-05-18 13:34:11

标签: python django python-requests

我有一个使用requests的Python脚本。它工作了很长时间。现在突然出现了以下错误。我尝试重新安装requests但是没有修复它。我能想到的唯一一个导致错误的是我一直在运行Django开发服务器,所以也许我被黑了?请帮忙。

代码:

import requests
...

错误:

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

1 个答案:

答案 0 :(得分:10)

您已使用名为random.py的本地文件屏蔽了内置库模块。重命名该文件。

Python查找要沿路径导入的模块,如果将模块放在标准库之前查看的位置,则最终可能会像在此处一样屏蔽内置模块。