Search code examples
pythonpython-3.xtwitter-oauth

Import twitter not working python


This is not working for me

import twitter

api= twitter.api(consumer_key,consumer_secret,access_token,access_token_secret)
//declared tokens value in the code.

It throws error : 'module' object is not callable

I do not want to use tweepy for this.


Solution

  • Unless I am misunderstanding which twitter library you are using, I think you want to be doing something like this (example retrieved by doing import twitter and then help(twitter) in an interactive Python interpreter):

    from twitter import *
    t = Twitter(auth=OAuth(token, token_key, con_secret, con_secret_key))
    

    In your case, you are getting and error because twitter.api is a submodule of the twitter module, and (as the Traceback says) is not callable.

    Since it appears that you have the necessary values, you should try plugging them in (in the correct order - not the difference between my example, and the order of the arguments in your example) to OAuth() or twitter.OAuth() if you don't do a wild import. Then you can pass that OAuth() instance to Twitter/twitter.Twitter() to create your API instance to query.

    So in your case, a (potentially) working example might be:

    import twitter
    
    auth = twitter.OAuth(access_token, access_token_secret, consumer_secret, consumer_key)
    api = twitter.Twitter(auth=auth)
    

    I am unable to test this, since I don't want to set up API credentials, but I have used this library in the past. I first learned about this API in this book chapter, which contains some helpful examples.