Forums

tweepy error?

Hi ! I've written this code to watch after different informations in tweetee but I get an error:

<pre>

import tweepy
fichier = open('/home/cpicard443/tweet.txt', 'w')
consumer_key = '*************'
consumer_secret = '**********'
access_token = '********'
access_token_secret = '**************'
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
userToExtractData = api.get_user('@etave45')
fichier.write("Description: " + userToExtractData.description)
fichier.write("User id: " + userToExtractData.id_str)
fichier.write("Language: " + userToExtractData.lang)
fichier.write("Account created at: " + str(userToExtractData.created_at))
if userToExtractData.location is not None:
    fichier.write("Location: " + userToExtractData.location)
if userToExtractData.time_zone is not None:
    fichier.write("Time zone: " + userToExtractData.time_zone)
fichier.write("Number of tweets: " + str(userToExtractData.statuses_count))
fichier.write("Number of followers: " + str(userToExtractData.followers_count))
fichier.write("Following: " + str(userToExtractData.friends_count))
fichier.write("A member of " + str(userToExtractData.listed_count) + " lists.")

statuses = api.user_timeline(id = userToExtractData.id, count = 2)
for status in statuses:
    fichier.write("***")
    fichier.write("Tweet id: " + status.id_str)
    fichier.write(status.text)

    fichier.write('\n')
    fichier.write("Retweet count: " + str(status.retweet_count))
    fichier.write("Favorite count: " + str(status.favorite_count))
    fichier.write(status.created_at)
    fichier.write("Status place: " + str(status.place))
    fichier.write("Source: " + status.source)
    fichier.write("Coordinates: " + str(status.coordinates))

</pre>

I get a Name error saying that the variable status is not defined (Traceback (most recent call last): File "<stdin>", line 7, in <module> NameError: name 'status' is not defined)

On my computer, the code is running but not here. Why?

That's not the code you're running. There's no reference to status anywhere near line 7, and your traceback seems to be referring to <stdin> How are you running the code?

I'm running the code in the console (copy/paste in the console). There is a reference to status here: for status in statuses:

I think Glenn is saying that in your code above, line 7 is auth = tweepy.OAuthHandler(consumer_key, consumer_secret), and not status. Do you have the exact code that your ran? Perhaps it is a line indentation problem? (eg: if you are outside of the for loop, or if you referenced status before the for loop, then 'status' would not be defined)