'模块对象没有属性'get'Python错误请求? [英] 'Module object has no attribute 'get' Python error Requests?
问题描述
我刚刚使用'easy_insatll'
安装了请求模块,我试图运行这个 tutrorial ,
i just installed the Requests module by using 'easy_insatll' and i tried to run the demo code of this tutrorial,
import requests
payload = {'username': 'xxxx', 'password': 'xxxxx'}
r = requests.get('https://github.com/timeline.json')
但是我收到此错误:
AttributeError:'module'对象没有属性'get'
任何想法?
谢谢
any idea? thanks
推荐答案
您正在从请求中导入所有名称
模块到您的本地命名空间,这意味着您不需要使用模块名称前缀它们:
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>
请注意,上述示例是我在翻译器中的测试中得到的。如果得到不同的结果,则导入错误的模块;检查您的python包中是否有额外的 requests.py
文件:
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
您还可以测试由请求提供的名称列表
模块:
>>> 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屋!