Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

api = twitter.Api() AttributeError: 'module' object has no attribute 'Api

I have been trying to write a simple mention grabber to get started with the twitter Api. Howsoever I've been experienceing some difficulties when initializing the Api. Running python2 on archlinux I installed twitter via easy_install, built it from source and installed it via pip. None of this seems to be working.

zergling :: ~/dev/kritter » python2
Python 2.7.2 (default, Jan 31 2012, 13:26:35) 
[GCC 4.6.2 20120120 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import twitter
>>> api = twitter.Api()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'Api'

Howsoever the pydoc for twitter is there. I have no clue what I'm doing wrong. I hope you can help

UPDATE: I tried using twitter.api() instead of twitter.Api() and got the following error: Traceback (most recent call last): File "main.py", line 8, in api = twitter.api() TypeError: 'module' object is not callable

Additional Informations:

>>> print dir(twitter)
['NoAuth', 'OAuth', 'Twitter', 'TwitterError', 'TwitterHTTPError', 'TwitterResponse',  'TwitterStream', 'UserPassAuth', '__all__', '__builtins__', '__doc__', '__file__',  '__name__', '__package__', '__path__', 'api', 'auth', 'oauth', 'read_token_file', 'stream', 'twitter_globals', 'write_token_file']
>>> print twitter.__path__
['/usr/lib/python2.7/site-packages/twitter-1.7.2-py2.7.egg/twitter']
like image 881
Momo Avatar asked Feb 15 '12 09:02

Momo


4 Answers

I think you've installed one twitter package, and look at another documentation. Ie: python-1.7.2 is the project from https://github.com/sixohsix/twitter, while you're looking at the http://code.google.com/p/python-twitter/ documentation. No match between both :)

So for the one you've installed, if you check the source code, a stream example is available, and other various examples in the pydoc:

  from twitter import Twitter
  # ...
  twitter = Twitter(
      auth=OAuth(token, token_key, con_secret, con_secret_key)))

  # Get the public timeline
  twitter.statuses.public_timeline()
like image 60
tito Avatar answered Nov 08 '22 11:11

tito


i found solution to this problem in following site

[http://himanen.info/solved-attributeerror-module-object-has-no-attribute-api/][1]

There are two Python libraries conflicting: twitter library and python-twitter library. The solution was quite straightforward:

pip uninstall twitter

Then I just made it sure that python-twitter was certainly installed:

pip install python-twitter

Thank you himanen it worked for me

like image 21
ayu for u Avatar answered Nov 08 '22 12:11

ayu for u


I got the same error because my python file was called twitter.py. It contained:

import twitter
api = twitter.Api (consumer_key=...

I renamed the file to twitterdata.py, removed twitter.pyc and then it worked.

like image 14
Paul Avatar answered Nov 08 '22 11:11

Paul


probably check twython, I have made all neccessary Ouath implementation using requests.

like image 1
Kracekumar Avatar answered Nov 08 '22 12:11

Kracekumar