无法导入Tweepy

时间:2015-08-22 05:07:00

标签: python twitter tweepy

我跟随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'])

3 个答案:

答案 0 :(得分:2)

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

pip install tweepy

对于v3.0

pip3 install tweepy

对于v3.5:

pip3.5 install tweepy

答案 1 :(得分:0)

我想你想尝试使用你的python版本的pip版本。如果你有python 2.7.7只运行:sudo pip2.7 install tweepy 它在python 2.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']
>>> 

答案 2 :(得分:0)

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

第2行

from tweepy import StreamListener

还使用IDE(首选PyCharm),因此在编码时不会犯这些小错误。