[英]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. 我已经安装了sixohsix的python twitter库( https://github.com/sixohsix/twitter )并尝试连接到Twitter,但它不起作用。 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.
我在stackOverflow上发现了一些帖子,但所有解决方案都不起作用。
From your traceback, I can see that your file is named twitter.py
: 从您的回溯中,我可以看到您的文件名为
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. 因此,
from twitter import *
的行试图将您自己脚本的内容导入到自身,而不是名为twitter
的库,因为当前目录通常优先于共享库。
Rename your own script to something that isn't identical to the library you're trying to use (eg my_twitter.py
), and things will work. 将您自己的脚本重命名为与您尝试使用的库不同的内容(例如
my_twitter.py
),事情就可以了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.