AttributeError: 'module' object has no attribute 'Consumer'
theasder opened this issue · comments
I tried to use oauth library for my application, that will parse tweets from an account, used instructions from here https://dev.twitter.com/oauth/overview/single-user:
def oauth_req(url, key, secret, http_method="GET", post_body=None, http_headers=None):
consumer = oauth.Consumer(key=key, secret=secret)
token = oauth.Token(key=key, secret=secret)
client = oauth.Client(consumer, token)
resp, content = client.request( url, method=http_method, body=post_body, headers=http_headers, force_auth_header=True )
return content
def get_tweets():
timeline = oauth_req('https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=climagic', '3WTohSoXhJrmmk61oOwmJBwQp', 'FlqER9N3qoYl89bPTZ8EP9CWXBtttkikaYeE9pqIyCwX7GSNYA' )
print(timeline)
But it doesn't work because of absence of attribute 'Consumer'.
Consumer is a class not an attribute and it does exist:
https://github.com/simplegeo/python-oauth2/blob/master/oauth2/__init__.py#L175
You probably just need to import oauth2
import oauth2 as oauth
I got an error
$ python3 script.py
Traceback (most recent call last):
File "script.py", line 1, in <module>
import clone
File "/Users/artemdremov/Documents/grabber/proglib/clone.py", line 7, in <module>
import oauth2 as oauth
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
File "<frozen importlib._bootstrap>", line 2222, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 2164, in _find_spec
File "<frozen importlib._bootstrap>", line 1940, in find_spec
File "<frozen importlib._bootstrap>", line 1916, in _get_spec
File "<frozen importlib._bootstrap>", line 1897, in _legacy_get_spec
File "<frozen importlib._bootstrap>", line 863, in spec_from_loader
File "<frozen importlib._bootstrap>", line 904, in spec_from_file_location
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/oauth2-1.5.211-py3.4.egg/oauth2/__init__.py", line 105
except UnicodeDecodeError, le:
^
SyntaxError: invalid syntax
I got the same issue:
AttributeError: 'module' object has no attribute 'Consumer'
But I imported it:
import oauth2 as oauth
I installed it earlier on through
sudo pip install python-oauth2
I also checked with print 'oauth2' in sys.modules
that the module is imported. Then by using print oauth
I got the path (/usr/local/lib/python2.7/dist-packages/oauth2/__init__.py)
to the module and realized, that it is a totally different one, than this one, which you refered to. Mine is only 230 lines.
Is it possible, that I somehow installed something wrong?
If the following is a workaround... I just downloaded the mentioned __init__.py
and replaced it on my computer. But now I get the error ImportError: No module named _version
. Do you have any information how or where to get that module? Google didn't provide me with useful information...
yeah, seems to work. I downloaded the __init__.py
and replaced mine with the new one. Then I commented these two lines out, since I didn't find any further usage of it
#import _version
#__version__ = _verion.__version__
And now it worked. HURRAY!
@xamiax if you're issue is resolved can you please close the issue? Thanks
I can't, since I didn't opened this issue...
glad my comment was helpful to someone! :D
I have the similar issue with Anaconda and Python 3.5. at my terminal, when i type pip3 install oauth2 - i could not find the repository. and i try to specify the exact wheel by downloading it from pypl wheel and install it (though it says python 3.4)...when i run the twitter sample code, it still gives me the same error message:
consumer = oauth2.Consumer(key=CONSUMER_KEY, secret=CONSUMER_SECRET)
AttributeError: module 'oauth2' has no attribute 'Consumer'
pip install oauth2
solved the problem for me.
i've downloaded it and replaced it, but it says ModuleNotFoundError: No module named 'oauth2._compat'