seeker
seeker

Reputation: 7011

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.

Upvotes: 4

Views: 8107

Answers (8)

Farid
Farid

Reputation: 11

I had similar problem. I had python-twitter package installed. I kept getting the same exact error. By removing python-twitter like:

(!pip uninstall python-twitter)

in Ipython console and installing twitter package now it works fine.

I hope that helps.

Upvotes: 1

user3797573
user3797573

Reputation: 11

I had the same problem and here is what helped me out. I first found out all the the packages that were related to twitter,which in my case were tweepy and twitter. I uninstalled both of them and re installed twitter. So far so good. It worked for me.

help("modules")

The main problem was not with the OAuth but installation of multiple twitter packages.

Upvotes: 1

Kuber
Kuber

Reputation: 1031

Instead of twitter.oauth.Oauth, use twitter.Api

import twitter
api = twitter.Api(consumer_key='consumer_key',
                      consumer_secret='consumer_secret',
                      access_token_key='access_token',
                      access_token_secret='access_token_secret')

Details Here

Upvotes: 1

Intrepid Traveller
Intrepid Traveller

Reputation: 95

If you are using oauth2 you may need to import as oauth in order for python to recognize the library. I had this same problem a few days ago and that was the simple fix.

Upvotes: 0

omarflorez
omarflorez

Reputation: 375

Just to give evidence that the solution provided by Rob, worked well for me. Apparently, the problem is that it was installed with apt-get and not pip, which may be give the right version of the library.

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

Upvotes: 0

alko
alko

Reputation: 48357

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).

Upvotes: 4

Robᵩ
Robᵩ

Reputation: 168836

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.

Upvotes: 3

Stedy
Stedy

Reputation: 7469

Try tweepy instead, I found it does a much better job of handling twitter authentication

import tweepy
CONSUMER_KEY = ''
CONSUMER_SECRET =''
OAUTH_TOKEN = ''
OAUTH_TOKEN_SECRET = ''
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(OAUTH_TOKEN, OAUTH_TOKEN_SECRET)

api = tweepy.API(auth)

Upvotes: 0

Related Questions