api=twitter.api()AttributeError:“module”对象没有“api”属性

2024-05-28 23:43:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在尝试编写一个简单的提到抓取器来开始使用twitter Api。不管怎样,我在初始化Api时遇到了一些困难。 在archlinux上运行python2,我通过easy_install安装twitter,从源代码构建并通过pip安装。这些似乎都不起作用。

zergling :: ~/dev/kritter » python2
Python 2.7.2 (default, Jan 31 2012, 13:26:35) 
[GCC 4.6.2 20120120 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import twitter
>>> api = twitter.Api()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'Api'

不管twitter的pydoc在哪里。我不知道我做错了什么。我希望你能帮忙

更新: 我尝试使用twitter.api()而不是twitter.Api(),得到以下错误: 回溯(最近一次呼叫时间): 文件“main.py”,第8行,in api=twitter.api() TypeError:“module”对象不可调用

其他信息:

>>> print dir(twitter)
['NoAuth', 'OAuth', 'Twitter', 'TwitterError', 'TwitterHTTPError', 'TwitterResponse',  'TwitterStream', 'UserPassAuth', '__all__', '__builtins__', '__doc__', '__file__',  '__name__', '__package__', '__path__', 'api', 'auth', 'oauth', 'read_token_file', 'stream', 'twitter_globals', 'write_token_file']
>>> print twitter.__path__
['/usr/lib/python2.7/site-packages/twitter-1.7.2-py2.7.egg/twitter']

Tags: installpippathintokenapi源代码easy
3条回答

我想您已经安装了一个twitter包,并查看了另一个文档。Ie:python-1.7.2是来自https://github.com/sixohsix/twitter的项目,而您正在查看http://code.google.com/p/python-twitter/文档。两者不匹配:)

因此,对于您安装的程序,如果检查源代码,就可以使用stream example和pydoc中的其他各种示例:

  from twitter import Twitter
  # ...
  twitter = Twitter(
      auth=OAuth(token, token_key, con_secret, con_secret_key)))

  # Get the public timeline
  twitter.statuses.public_timeline()

我得到了同样的错误,因为我的python文件名为twitter.py。它包含:

import twitter
api = twitter.Api (consumer_key=...

我将文件重命名为twitterdata.py,删除了twitter.pyc,然后它就工作了。

我在下面的站点找到了这个问题的解决方案

[http://himanen.info/solved-attributeerror-module-object-has-no-attribute-api/][1]

有两个Python库存在冲突:twitter库和Python twitter库。解决方法非常简单:

pip uninstall twitter

然后我确定python twitter确实安装了:

pip install python-twitter

谢谢他,这对我有用

相关问题 更多 >

    热门问题