python请求导入错误

标签 python request attributeerror

当我尝试导入 requests 模块时:

import requests
r = requests.get('https://github.com/timeline.json')

我收到这个错误:

Traceback (most recent call last):
  File "C:\Users\gek0n\Desktop\requests.py", line 1, in <module>
    import requests
  File "C:\Users\gek0n\Desktop\requests.py", line 3, in <module>
    r = requests.get('https://github.com/timeline.json')
AttributeError: 'module' object has no attribute 'get'

然后我检查了 Stack Overflow 并发现了这个:

print dir(requests)

而不是这个:

['ConnectionError', 'HTTPError', 'Request', 'RequestException', 'Response', 'Session', 'Timeout', 'TooManyRedirects', 'URLRequired', '__author__', '__build__', '__builtins__', '__copyright__', '__doc__', '__file__', '__license__', '__name__', '__package__', '__path__', '__title__', '__version__', '_oauth', 'api', 'auth', 'certs', 'codes', 'compat', 'cookies', 'defaults', 'delete', 'exceptions', 'get', 'head', 'hooks', 'models', 'options', 'packages', 'patch', 'post', 'put', 'request', 'safe_mode', 'session', 'sessions', 'status_codes', 'structures', 'utils']

我有这个:

['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'requests']
['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'requests']

是的!有两个字符串,只有六个元素。这是为什么?出了什么问题,我该如何解决?

最佳答案

您将脚本命名为 requests.py 并且正在导入它而不是库。

因为您的 import requests 行是模块中的第一行,它也是在再次导入自身时在您的模块中找到的唯一对象(除了标准的双下划线值),这就是为什么您会在 dir() 输出中看到 'requests' 的原因。

重命名您的脚本。

关于python请求导入错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22413483/

相关文章:

python - 如何将模式 w{3,3} 替换为 w{3}

node.js - 对 API 的请求在浏览器中成功但在 Node 中失败

PyCharm:模块 'math' 没有属性 'prod'

Python-属性错误: index

python - 为什么 (-3//2 = -2)

python - 将函数用于模拟类的方法返回值

node.js - 如何在 Promisify 请求模块后正确使用 putAsync

python - AttributeError: 'NoneType' 对象没有属性 'add'

python - 如何自定义 virtualenv shell 提示符

reactjs - 在循环中使用 axios 发出多个 get 请求的最佳方法是什么?