19

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']
Momo
  • 311
  • 1
  • 3
  • 9
  • 1
    try to "print dir(twitter)" and "print twitter.__path__", can see what's the methods available from the module, and where it's located (to be sure you're using the right one.) – tito Feb 15 '12 at 09:53
  • @tito: got the following out of this. using twitter.api() i get another error beeing: `api = twitter.api(); TypeError: 'module' object is not callable` – Momo Feb 15 '12 at 09:59
  • Well, you see that their is no Api or api class/func in the dir() – tito Feb 15 '12 at 10:19
  • I'm sure you're not looking the right documentation for that module. There is many twitter python wrapper, and look like this one is different :) – tito Feb 15 '12 at 10:22
  • 1
    The twitter-1.7.2 is not the same as http://code.google.com/p/python-twitter, so don't look over that doc, it's a different project – tito Feb 15 '12 at 10:22

7 Answers7

19

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()
tito
  • 11,887
  • 1
  • 49
  • 69
  • thank you, this totally helped. i just thought easy_install would make my live easy. – Momo Feb 28 '12 at 21:01
  • To understand what happened, if you are using [ipython](http://ipython.org/), you can print the object "twitter" and see where it comes from. If it's an .egg file and it's python_twitter, you might have to follow the easy steps described here (it worked for me, I had installed python_twitter using easy_install): http://stackoverflow.com/questions/1231688/how-do-i-remove-packages-installed-with-pythons-easy-install – c-a Mar 05 '14 at 02:08
  • 8
    I had installed with `pip install twitter`, when it should've been `pip install python-twitter` – thameera Apr 16 '14 at 14:02
  • this suggests he use another API. You should answer his question and not propose another product. The correct answer is by "ayu for u" below. – Walker Rowe Jan 26 '19 at 18:44
15

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

ayu for u
  • 179
  • 1
  • 4
11

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.

Paul
  • 539
  • 6
  • 8
1

If you are using the python-twitter wrapper. during installation process it must have created an eggfile with the name twitter.py.egg rename it by some other name like help.py.egg it will work for you.

Sourabh Choudhary
  • 207
  • 1
  • 5
  • 18
1

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

Kracekumar
  • 16,101
  • 10
  • 41
  • 51
0

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 really works

0

Use module python-twitter instead of twitter

$ pip install python-twitter

Reference : https://python-twitter.readthedocs.io/en/latest/installation.html

For Modules Documentation : https://python-twitter.readthedocs.io/en/latest/twitter.html#modules-documentation