2

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.

4

1 回答 1

8

从您的回溯中,我可以看到您的文件名为twitter.py

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

因此,该行from twitter import *尝试将您自己的脚本的内容导入到自身,而不是名为 的库twitter中,因为当前目录通常优先于共享库。

将您自己的脚本重命名为与您尝试使用的库不同的名称(例如my_twitter.py),这样就可以了。

于 2013-06-22T18:38:14.200 回答