Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twitter module python 'module' object has no attribute Oauth

Im trying to follow this basic example here.

Code

import twitter

# XXX: Go to http://dev.twitter.com/apps/new to create an app and get values
# for these credentials, which you'll need to provide in place of these
# empty string values that are defined as placeholders.
# See https://dev.twitter.com/docs/auth/oauth for more information 
# on Twitter's OAuth implementation.

CONSUMER_KEY = ''
CONSUMER_SECRET =''
OAUTH_TOKEN = ''
OAUTH_TOKEN_SECRET = ''

auth = twitter.oauth.OAuth(OAUTH_TOKEN, OAUTH_TOKEN_SECRET,
                           CONSUMER_KEY, CONSUMER_SECRET)

twitter_api = twitter.Twitter(auth=auth)

# Nothing to see by displaying twitter_api except that it's now a
# defined variable

print twitter_api

But running the example throws the following error.

vagrant@lucid32:~$ python twitter.py
Traceback (most recent call last):
  File "twitter.py", line 1, in <module>
    import twitter
  File "/home/vagrant/twitter.py", line 14, in <module>
    auth = twitter.oauth.OAuth(OAUTH_TOKEN, OAUTH_TOKEN_SECRET,
AttributeError: 'module' object has no attribute 'oauth'

Any help would be appreciated.

like image 928
seeker Avatar asked Dec 26 '22 15:12

seeker


2 Answers

Don't name your scripts the same as modules you're import, as when python imports it first looks in script directory, so twitter.py is imported instead of twitter module.

And after you rename your file to something different with twitter.py, don't forget to remove it's compiled code (twitter.pyc and tweepy.pyc).

like image 92
alko Avatar answered Feb 23 '23 19:02

alko


You don't mention what OS you are using. Is it Ubuntu or Debian?

If so, apt-get install python-twitter installs the wrong package for your needs. Install the correct package like so:

sudo apt-get purge python-twitter
sudo pip install twitter 

Additionally, you should change the name of your program so that it isn't identical to the module you are importing.

like image 27
Robᵩ Avatar answered Feb 23 '23 21:02

Robᵩ