Igeloeffnung
Igeloeffnung

Reputation: 31

Python Twitter: name Twitter is not defined

I've installed the python twitter library of sixohsix (https://github.com/sixohsix/twitter) and tried to connect to Twitter, but it doesn't work. This is my code:

#!/usr/bin/env python

from twitter import *

OAUTH_TOKEN = '...'
OAUTH_SECRET = '...'
CONSUMER_KEY = '...'
CONSUMER_SECRET = '...'

t = twitter.Twitter(auth=OAuth(OAUTH_TOKEN, OAUTH_SECRET, CONSUMER_KEY, CONSUMER_SECRET))

# Get your "home" timeline
t.statuses.home_timeline()

The error message I get is:

Traceback (most recent call last):
   File "./twitter.py", line 3, in <module>
       from twitter import *
   File "/home/XXX/twitter.py", line 11, in <module>
       t = Twitter(auth=OAuth(OAUTH_TOKEN, OAUTH_SECRET, CONSUMER_KEY, CONSUMER_SECRET))
NameError: name 'Twitter' is not defined

I don't know why this error occurs. I also tried

t = twitter.Twitter(...)

but it doesn't work either. I found some posts at stackOverflow, butt all solutions doesn't work.

Upvotes: 2

Views: 7826

Answers (1)

Amber
Amber

Reputation: 527328

From your traceback, I can see that your file is named twitter.py:

File "/home/XXX/twitter.py", line 11, in <module>

Because of this, the line from twitter import * is attempting to import the contents of your own script into itself, rather than the library named twitter, because the current directory generally has precedence over shared libraries.

Rename your own script to something that isn't identical to the library you're trying to use (e.g. my_twitter.py), and things will work.

Upvotes: 8

Related Questions