"Attributeerror: Module 'Tweepy' Has No Attribute 'Streamlistener'" with Python 3.9
class MyStreamListener(tweepy.StreamListener):
    def on_status(self, status):
        print(status.text)  # prints every tweet received

    def on_error(self, status_code):
        if status_code == 420:  # end of monthly limit rate (500k)
            return False

I use Python 3.9 and installed Tweepy via pip. I get the AttributeError on the class line. My import is just import tweepy. Authentication gets correctly handled. In the streaming.py file, I have the class Stream. But using this class ends in that. There is for example no status.text, even if there is the on_status function. I am a little bit confused.

1

3 Answers

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

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

1

As mentioned by @Harmon758, they merged StreamListener into Stream after version 4. Also you don`t need to create the api auth object separately. Here is the code:

from tweepy import Stream

class MyStreamListener(Stream):
    def on_status(self, status):
        print(status.text)  # prints every tweet received

    def on_error(self, status_code):
        if status_code == 420:  # end of monthly limit rate (500k)
            return False


stream = MyStreamListener('consumer_key',
                          'consumer_secret',
                          'access_token',
                          'access_token_secret')

stream.filter(track=["Python"], languages=["en"])

If you look at the modules, the correct way to reference StreamListener is tweepy.streaming.StreamListener, not tweepy.StreamListener.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Marcus Vance

Marcus Vance

Cybersecurity & Digital Privacy Researcher

Marcus Vance is a cybersecurity auditor and technology writer dedicated to educating the public about online safety, data privacy regulations, enterprise security, and emerging cyber threats.

Share this article
Twitter Facebook Pinterest