1

hi i am new to python and i got this error but i have installed twitter but it's giving this error

import twitter
api=twitter.Api()
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
api=twitter.Api()
AttributeError:'module' object has no attribute 'Api'

I don't know about this error as i have almost every package related to twitter

Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    api = twitter.Api()
AttributeError: 'module' object has no attribute 'Api'

when i give this command python setup.py install_data it give error like this running install_data

Traceback (most recent call last) :
file "setup.py" ,line 47 , <module>
""" ,
File "C:\python26\lib\distutils\core.py" ,line 152 , in setup
dist.run_commands()
File "C:\python26\lib\distutils\dist.py" ,line 975 , in run_commands
self.run_command(cmd)
File "C:\python26\lib\distutils\dist.py" ,line 995 , in run_commands
command_obj.run
File "C:\python26\lib\distutils\command\install_data.py" , line 44 in run
For f in self.data_files

5 Answers5

1

can you reinstall using,

sudo pip install twitter

or

sudo easy_install twitter

The old versions did not required OAuth but new one does. The documentation of the latest version requires these to initiate the API

>>> api = twitter.Api(consumer_key='consumer_key',

    consumer_secret='consumer_secret', access_token_key='access_token', access_token_secret='access_token_secret') 
chheplo
  • 211
  • 3
  • 14
  • i am window 7 user and i think sudo is for linux ?? –  Aug 06 '12 at 21:12
  • I think you can still do "easy_install twitter" use this link to install setuptools for windows http://packages.python.org/distribute/easy_install.html#installing-easy-install – chheplo Aug 06 '12 at 21:13
  • I'm seeing the same behavior as the OP on win7/64 and debian, both freshly installed (pip install -U twitter). `import twitter;twitter.Api` gives `AttributeError`. I haven't used the module before, but from reading the start of the docs, it looks like you might have to do an OAuth authentication dance first..? – thebjorn Aug 06 '12 at 21:19
  • Actually I had the old version which did not required OAuth stuff. I installed the latest one. The documentation of the new version mention doing this. >>> api = twitter.Api(consumer_key='consumer_key', consumer_secret='consumer_secret', access_token_key='access_token', access_token_secret='access_token_secret') – chheplo Aug 06 '12 at 21:23
  • Apart from the search... Twitter requires tokens it is documented – Jon Clements Aug 06 '12 at 21:24
  • the OP is reading the docs of python_twitter, not twitter :-) They both install under the name twitter, but only python_twitter has a `twitter.Api()` call that takes no arguments. – thebjorn Aug 06 '12 at 21:52
1

Your mixed with libraries actually the problem is that your are reading library of python_twitter and installed twitter.You need select the correct documentation.This is documentation error nothing else.You installed correct library.

0

The error message "'module' object has no attribute 'Api'" means what it says: Python can't find anything named 'Api' inside the imported module 'twitter'.

Try dir(twitter) after the import statement. dir() will show what Python finds inside the object. If dir(twitter) shows an object called 'twitter', you may have to do something like from twitter import twitter.

Lenna
  • 1,379
  • 8
  • 21
  • ['NoAuth', 'OAuth', 'Twitter', 'TwitterError', 'TwitterHTTPError', 'TwitterResponse', 'TwitterStream', 'UserPassAuth', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', 'api', 'auth', 'dedent', 'oauth', 'oauth_dance', 'oauth_doc', 'read_token_file', 'stream', 'twitter_globals', 'write_token_file'] –  Aug 06 '12 at 21:51
  • ImportError: cannot import name twitter –  Aug 06 '12 at 22:02
  • @munieb: Python is case sensitive. Notice that `dir(twitter)` contains 'Twitter', not 'twitter', and 'api', not 'Api'. – Lenna Aug 06 '12 at 23:26
  • now i get this ['ACCESS_TOKEN_URL', 'AUTHORIZATION_URL', 'Api', 'CHARACTER_LIMIT', 'DEFAULT_CACHE', 'DirectMessage', 'Hashtag', 'List', 'REQUEST_TOKEN_URL', 'SIGNIN_URL', 'Status', 'StringIO', 'Trend', 'TwitterError', 'Url', 'User', '_FileCache', '_FileCacheError', '__author__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__version__', 'base64', 'calendar', 'datetime', 'gzip', 'httplib', 'md5', 'oauth', 'os', 'parse_qs', 'parse_qsl', 'rfc822', 'simplejson', 'sys', 'tempfile', 'textwrap', 'time', 'urllib', 'urllib2', 'urlparse'] why i get Twittererror in it –  Aug 07 '12 at 12:53
0

Where did you install the python twitter module from? I used http://code.google.com/p/python-twitter/source/browse/twitter.py, and it has an Api class.

>>> import twitter
>>> api = twitter.Api()
Brenden Brown
  • 2,869
  • 1
  • 12
  • 15
  • i installed it from python official site . well i copy this code but it not run –  Aug 06 '12 at 22:03
0

You've installed the wrong twitter library. I'm guessing you've done

pip install twitter

you should uninstall that library:

pip uninstall twitter

and install the correct(*) one

pip install python_twitter

(*) by correct I mean the one you're reading the documentation to :-)

thebjorn
  • 22,303
  • 7
  • 72
  • 116
  • I tested it on win7, so yes, it does work. If you've used easy_install to install it instead of pip, then I'm not sure if there is an easy way to uninstall (someone else might know though). – thebjorn Aug 06 '12 at 21:59
  • Ah.. Save this file ( https://raw.github.com/pypa/pip/master/contrib/get-pip.py ) as `c:\get-pip.py` and then run `python c:\get-pip.py` to install pip. – thebjorn Aug 06 '12 at 22:03
  • More advice on installing pip on windows found here: http://stackoverflow.com/questions/4750806/how-to-install-pip-on-windows – thebjorn Aug 06 '12 at 22:07
  • i install pip when i am using this command pip install python_twitter but it give error that no such file or directory !! –  Aug 06 '12 at 23:57
  • He used setup.py install to install it, see his other questions. – Lennart Regebro Aug 07 '12 at 07:28
  • @LennartRegebro i installed it . i have no issue on installion . but i have issue on running pip –  Aug 07 '12 at 11:42
  • @munieb: pip is used to install things. You have already installed your module, you don't need pip, at least not yet. The question is now: did you install the right thing? Did you install the module called "python_twitter" or the one called "twitter"? What was the filename of the package you installed? – Lennart Regebro Aug 07 '12 at 11:49
  • @LennartRegebro i also got the error about Twitter .. is there difference between twitter and Twitter ? –  Aug 07 '12 at 12:52
  • @munieb: Then all the other comments above are correct: You installed the module called "twitter" but you are reading the documentation for a completely different module, called "python_twitter". Confusing, yes. Read the documentation on the page you linked to above instead. And yes, there is a difference. "Twitter" starts with an uppercase T and "twitter" starts with a lower-case t. Python is, as others have pointed out case sensitive. "Twitter" and "twitter" are two different names. – Lennart Regebro Aug 07 '12 at 13:16
  • @LennartRegebro so i want to get trending topics from twitter which package should i use ? –  Aug 07 '12 at 13:36
  • @munieb: 1. I don't know. 2. That's a separate question, do one question per post. 3. The problem is not that you choose the wrong one. The problem is that you installed one, and you are reading the documentation for another. Which you now have been told multiple times. I think you need to read your asnwer more closely, it's annoying that we have to nag and repeat ourselves. – Lennart Regebro Aug 07 '12 at 14:31