Can’t import StreamListener

We Are Going To Discuss About Can’t import StreamListener. So lets Start this Python Article.

Can’t import StreamListener

  1. How to solve Can't import StreamListener

    Tweepy v4.0.0 was released yesterday and it merged StreamListener into Stream.
    I recommend updating your code to subclass Stream instead.
    Alternatively, you can downgrade to v3.10.0.

  2. Can't import StreamListener

    Tweepy v4.0.0 was released yesterday and it merged StreamListener into Stream.
    I recommend updating your code to subclass Stream instead.
    Alternatively, you can downgrade to v3.10.0.

Solution 1

Tweepy v4.0.0 was released yesterday and it merged StreamListener into Stream.

I recommend updating your code to subclass Stream instead.
Alternatively, you can downgrade to v3.10.0.

Original Author Harmon758 Of This Content

Solution 2

For Tweepy v4, you don’t need to use StreamListener. The following is an edited code for v4:

import tweepy

class MyListener(tweepy.Stream):
    def on_data(self, data):
        try:
            with open('python.json', 'a') as f:
                f.write(data)
                return True
        except BaseException as e:
            print("Error on_data: %s" % str(e))
        return True
 
    def on_error(self, status):
        print(status)
        return True
 
twitter_stream = MyListener(
  "Consumer Key here", "Consumer Secret here",
  "Access Token here", "Access Token Secret here"
)
twitter_stream.filter(track=['#python'])

Original Author AKMalkadi Of This Content

Solution 3

A slight tweak will fix this

#from tweepy.streaming import StreamListener #OLD CODE

import tweepy # NEW CODE

#class TweetListener (StreamListener): #OLD CODE

class TweetListener (tweepy.Stream): # NEW CODE

Original Author Saurabh Sinha Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

ittutorial team

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment