【问题标题】:api = twitter.Api() AttributeError: 'module' object has no attribute 'Apiapi = twitter.Api() AttributeError: 'module' 对象没有属性 'Api
【发布时间】:2012-03-06 15:52:10
【问题描述】:

我一直在尝试编写一个简单的提及抓取器来开始使用 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 行,在 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']

【问题讨论】:

  • 尝试“print dir(twitter)”和“print twitter.__path__”,可以看到模块中可用的方法是什么,以及它的位置(确保你使用的是正确的.)
  • @tito:从中得到以下信息。使用 twitter.api() 我收到另一个错误:api = twitter.api(); TypeError: 'module' object is not callable
  • 好吧,你看到他们在 dir() 中没有 Api 或 api 类/函数
  • 我确定您没有为该模块寻找正确的文档。有很多 twitter python 包装器,看起来这个是不同的:)
  • twitter-1.7.2 和code.google.com/p/python-twitter 不一样,所以不要看那个文档,它是一个不同的项目

标签: python api twitter archlinux


【解决方案1】:

我想你已经安装了一个 twitter 包,然后查看另一个文档。即: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()

【讨论】:

  • 谢谢,这完全有帮助。我只是认为 easy_install 会让我的生活变得轻松。
  • 要了解发生了什么,如果您使用的是ipython,您可以打印对象“twitter”并查看它的来源。如果它是一个 .egg 文件并且是 python_twitter,您可能必须按照此处描述的简单步骤进行操作(它对我有用,我已经使用 easy_install 安装了 python_twitter):stackoverflow.com/questions/1231688/…
  • 我已经安装了pip install twitter,而应该是pip install python-twitter
  • 这表明他使用了另一个 API。你应该回答他的问题,而不是提出另一个产品。正确答案是下面的“ayu for u”。
【解决方案2】:

可能检查twython,我已经使用requests 完成了所有必要的Ouath 实现。

【讨论】:

    【解决方案3】:

    如果您使用的是 python-twitter 包装器。在安装过程中,它必须创建一个名为 twitter.py.egg 的 eggfile,将其重命名为 help.py.egg 等其他名称,它会为您工作。

    【讨论】:

      【解决方案4】:

      我在以下站点找到了解决此问题的方法

      [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
      

      谢谢你,它对我有用

      【讨论】:

      • 谢谢,就是这样。
      【解决方案5】:

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

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

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

      【讨论】:

        【解决方案6】:

        我在以下站点找到了解决此问题的方法

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

        有两个 Python 库冲突:twitter 库和 python-twitter 库。解决方案非常简单:

        pip 卸载推特 然后我只是确定 python-twitter 确实安装了:

        pip 安装 python-twitter 谢谢himanen..它真的有效

        【讨论】:

        • 您分享的链接已损坏:“找不到您请求的页面”
        【解决方案7】:

        使用模块 python-twitter 而不是 twitter

        $ pip install python-twitter
        

        参考:https://python-twitter.readthedocs.io/en/latest/installation.html

        对于模块文档:https://python-twitter.readthedocs.io/en/latest/twitter.html#modules-documentation

        【讨论】:

          猜你喜欢
          • 2015-07-11
          • 2021-01-15
          • 2015-05-16
          • 1970-01-01
          • 2022-12-20
          • 2018-01-14
          • 2014-08-15
          • 2016-07-17
          • 2015-05-26
          相关资源
          最近更新 更多