【问题标题】:Twitter module python 'module' object has no attribute OauthTwitter模块python“模块”对象没有属性Oauth
【发布时间】:2014-01-25 14:09:15
【问题描述】:

我正在尝试遵循这个基本示例here。

代码

import twitter

# XXX: Go to http://dev.twitter.com/apps/new to create an app and get values
# for these credentials, which you'll need to provide in place of these
# empty string values that are defined as placeholders.
# See https://dev.twitter.com/docs/auth/oauth for more information 
# on Twitter's OAuth implementation.

CONSUMER_KEY = ''
CONSUMER_SECRET =''
OAUTH_TOKEN = ''
OAUTH_TOKEN_SECRET = ''

auth = twitter.oauth.OAuth(OAUTH_TOKEN, OAUTH_TOKEN_SECRET,
                           CONSUMER_KEY, CONSUMER_SECRET)

twitter_api = twitter.Twitter(auth=auth)

# Nothing to see by displaying twitter_api except that it's now a
# defined variable

print twitter_api

但运行示例会引发以下错误。

vagrant@lucid32:~$ python twitter.py
Traceback (most recent call last):
  File "twitter.py", line 1, in <module>
    import twitter
  File "/home/vagrant/twitter.py", line 14, in <module>
    auth = twitter.oauth.OAuth(OAUTH_TOKEN, OAUTH_TOKEN_SECRET,
AttributeError: 'module' object has no attribute 'oauth'

任何帮助将不胜感激。

【问题讨论】:

  • 我发现 twitter.API 即使在安装了 twitter 包而不是 python-twitter 之后也对我有用

标签: python twitter oauth oauth-2.0


【解决方案1】:

不要将脚本命名为与要导入的模块相同的名称,因为当 python 导入时,它首先会在脚本目录中查找,因此会导入 twitter.py 而不是 twitter 模块。

在您将文件重命名为与 twitter.py 不同的名称后,不要忘记删除它的编译代码(twitter.pyc 和 tweepy.pyc)。

【讨论】:

    【解决方案2】:

    您没有提及您使用的是什么操作系统。是 Ubuntu 还是 Debian?

    如果是这样,apt-get install python-twitter 会根据您的需要安装错误的软件包。像这样安装正确的包:

    sudo apt-get purge python-twitter
    sudo pip install twitter 
    

    此外,您应该更改程序的名称,使其与您正在导入的模块不同。

    【讨论】:

    • 重命名它,并没有改变任何东西。 twit.py 有效吗?
    • @KodeSeeker - 除了我之前的建议,我怀疑你安装了错误的 twitter API。
    【解决方案3】:

    使用 twitter.Api 代替 twitter.oauth.Oauth

    import twitter
    api = twitter.Api(consumer_key='consumer_key',
                          consumer_secret='consumer_secret',
                          access_token_key='access_token',
                          access_token_secret='access_token_secret')
    

    Details Here

    【讨论】:

      【解决方案4】:

      我遇到了同样的问题,这就是帮助我的方法。 我首先找到了所有与 twitter 相关的包,在我的例子中是 tweepy 和 twitter。我卸载了它们并重新安装了 twitter。到目前为止,一切都很好。它对我有用。

      帮助(“模块”)

      主要问题不在于 OAuth,而是安装了多个 twitter 包。

      【讨论】:

        【解决方案5】:

        我有类似的问题。我安装了python-twitter 软件包。我一直收到同样的错误。通过删除python-twitter 喜欢:

        (!pip uninstall python-twitter)
        

        在Ipython 控制台并安装 twitter 包现在可以正常工作了。

        希望对你有帮助。

        【讨论】:

          【解决方案6】:

          改用tweepy,我发现它在处理 twitter 身份验证方面做得更好

          import tweepy
          CONSUMER_KEY = ''
          CONSUMER_SECRET =''
          OAUTH_TOKEN = ''
          OAUTH_TOKEN_SECRET = ''
          auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
          auth.set_access_token(OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
          
          api = tweepy.API(auth)
          

          【讨论】:

          • 这给了我这个错误。 中的文件“tweepy.py”,第 1 行 import tweepy 文件“/home/vagrant/tweepy.py”,第 6 行,在 中 auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET) AttributeError: 'module'对象没有属性“OAuthHandler”
          • 你是如何安装 tweepy 的?看起来 python 正在查看你的主目录
          • @KodeSeeker - 我不打算打死马,但您不能将您的程序命名为与您正在导入的模块相同的名称。
          • @Robᵩ 似乎 OP 连续做了两次 :)
          【解决方案7】:

          只是为了证明 Rob 提供的解决方案对我很有效。显然,问题在于它是使用 apt-get 而不是 pip 安装的,这可能会提供正确版本的库。

          sudo apt-get purge python-twitter
          sudo pip install twitter 
          

          【讨论】:

            【解决方案8】:

            如果您使用的是 oauth2,您可能需要导入为 oauth,以便 python 识别该库。几天前我也遇到了同样的问题,这就是简单的解决方法。

            【讨论】:

              猜你喜欢
              • 2014-10-26
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2013-12-14
              • 2015-03-09
              • 2013-07-05
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多