无法导入Tweepy

2024-04-29 09:14:23 发布

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

我正在跟踪this tutorial并且我被Tweepy安装卡住了。我按照Tweepy's github page中的说明运行了sudo pip install tweepy,安装成功:

成功安装

User-MBP:Mercuria user$ sudo pip install tweepy
Downloading/unpacking tweepy
  Downloading tweepy-3.4.0-py2.py3-none-any.whl
Downloading/unpacking requests>=2.4.3 (from tweepy)
  Downloading requests-2.7.0-py2.py3-none-any.whl (470kB): 470kB downloaded
Downloading/unpacking six>=1.7.3 (from tweepy)
  Downloading six-1.9.0-py2.py3-none-any.whl
Downloading/unpacking requests-oauthlib>=0.4.1 (from tweepy)
  Downloading requests_oauthlib-0.5.0-py2.py3-none-any.whl
Downloading/unpacking oauthlib>=0.6.2 (from requests-oauthlib>=0.4.1->tweepy)
  Downloading oauthlib-1.0.3.tar.gz (109kB): 109kB downloaded
  Running setup.py (path:/private/tmp/pip_build_root/oauthlib/setup.py) egg_info for package oauthlib

Installing collected packages: tweepy, requests, six, requests-oauthlib, oauthlib
  Found existing installation: requests 2.1.0
    Uninstalling requests:
      Successfully uninstalled requests
  Found existing installation: requests-oauthlib 0.4.0
    Uninstalling requests-oauthlib:
      Successfully uninstalled requests-oauthlib
  Found existing installation: oauthlib 0.6.1
    Uninstalling oauthlib:
      Successfully uninstalled oauthlib
  Running setup.py install for oauthlib

Successfully installed tweepy requests six requests-oauthlib oauthlib
Cleaning up...

异常

User-MBP:Mercuria user$ python feed.py 
Traceback (most recent call last):
  File "feed.py", line 2, in <module>
    from tweepy.streaming import StreamListener
ImportError: No module named tweepy.streaming

我正在运行的代码可以在教程页面中找到,也粘贴在下面:

代码

#Import the necessary methods from tweepy library
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream

#Variables that contains the user credentials to access Twitter API 
access_token = "ENTER YOUR ACCESS TOKEN"
access_token_secret = "ENTER YOUR ACCESS TOKEN SECRET"
consumer_key = "ENTER YOUR API KEY"
consumer_secret = "ENTER YOUR API SECRET"


#This is a basic listener that just prints received tweets to stdout.
class StdOutListener(StreamListener):

    def on_data(self, data):
        print data
        return True

    def on_error(self, status):
        print status


if __name__ == '__main__':

    #This handles Twitter authetification and the connection to Twitter Streaming API
    l = StdOutListener()
    auth = OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)
    stream = Stream(auth, l)

    #This line filter Twitter Streams to capture data by the keywords: 'python', 'javascript', 'ruby'
    stream.filter(track=['python', 'javascript', 'ruby'])

Tags: frompytokennoneaccesspy3anyrequests
3条回答

有同样的问题。 对我来说,这是因为我的系统上安装了多个版本的python。 我必须做以下事情来修复它: 对于2.7版

pip install tweepy

对于v3.0

pip3 install tweepy

对于v3.5:

pip3.5 install tweepy

我想您应该尝试使用python版本的pip版本。如果您有python 2.7.7,只需运行:sudo pip2.7 install tweepy 它在Python2.7.6中对我有效。

>>> from tweepy.streaming import StreamListener
>>> dir(StreamListener)
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'keep_alive', 'on_connect', 'on_data', 'on_delete', 'on_direct_message', 'on_disconnect', 'on_error', 'on_event', 'on_exception', 'on_friends', 'on_limit', 'on_status', 'on_timeout', 'on_warning']

为什么你要试着从tweepy.streaming而不是tweepy?

Python 2.7.6 (default, Sep  9 2014, 15:04:36) 
>>> import tweepy
>>> from tweepy import StreamListener
>>> dir(StreamListener)
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'keep_alive', 'on_connect', 'on_data', 'on_delete', 'on_direct_message', 'on_disconnect', 'on_error', 'on_event', 'on_exception', 'on_friends', 'on_limit', 'on_status', 'on_timeout', 'on_warning']
>>> 

StreamListenertweepy包中的一个类,因此需要从tweepy而不是从tweepy.stream导入它,请使用正确的导入修改代码。

第2行

from tweepy import StreamListener

也可以使用IDE(首选PyCharm),这样在编写代码时就不会犯这些小错误。

相关问题 更多 >