'模块对象没有属性'get' Python 错误请求? [英] 'Module object has no attribute 'get' Python error Requests?

查看:25
本文介绍了'模块对象没有属性'get' Python 错误请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚使用 easy_install 安装了 Requests 模块我尝试运行此教程<的演示代码/a>,

导入请求有效载荷 = {'用户名':'xxxx','密码':'xxxxx'}r = requests.get('https://github.com/timeline.json')

但我收到此错误:

<块引用>

AttributeError: 'module' 对象没有属性 'get'

解决方案

您正在将 requests 模块中的所有名称导入到您的本地命名空间中,这意味着您不再需要为它们添加前缀模块名称:

<预><代码>>>>从请求导入 *>>>得到<函数在 0x107820b18 处获取>

如果您要使用 import requests 语句导入模块,则您将模块本身添加到您的命名空间,并且必须使用全名:

<预><代码>>>>进口请求>>>请求.get<函数在 0x102e46b18 处获取>

请注意,上面的示例是我从解释器中的测试中得到的.如果得到不同的结果,则说明您导入了错误的模块;检查你的 python 包中是否有一个额外的 requests.py 文件:

<预><代码>>>>进口请求>>>打印请求.__file__/private/tmp/requeststest/lib/python2.7/site-packages/requests/__init__.pyc

您还可以测试 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', '补丁', 'post', 'put', 'request', 'safe_mode', 'session', 'sessions', 'status_codes', 'structures', 'utils']

I just installed the Requests module by using easy_install and I tried to run the demo code of this tutorial,

import requests
payload = {'username': 'xxxx', 'password': 'xxxxx'}
r = requests.get('https://github.com/timeline.json')

but I get this error:

AttributeError: 'module' object has no attribute 'get'

解决方案

You are importing all names from the requests module into your local namespace, which means you do not need to prefix them anymore with the module name:

>>> from requests import *
>>> get
<function get at 0x107820b18>

If you were to import the module with an import requests statement instead, you added the module itself to your namespace and you do have to use the full name:

>>> import requests
>>> requests.get
<function get at 0x102e46b18>

Note that the above examples is what I got from my tests in the interpreter. If you get different results, you are importing the wrong module; check if you have an extra requests.py file in your python package:

>>> import requests
>>> print requests.__file__
/private/tmp/requeststest/lib/python2.7/site-packages/requests/__init__.pyc

You can also test for the name listing provided by the requests module:

>>> 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']

这篇关于'模块对象没有属性'get' Python 错误请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆