How To Fix Tweepy AttributeError API object has no attribute search

Make sure you have latest version of tweepy installed.

In [7]:
pip install tweepy

Let us check the version of tweepy installed.

In [8]:
pip show tweepy
Name: tweepy
Version: 4.10.1
Summary: Twitter library for Python
Home-page: https://www.tweepy.org/
Author: Joshua Roesslein
Author-email: [email protected]
License: MIT
Location: /home/anaconda3/lib/python3.9/site-packages
Requires: requests-oauthlib, oauthlib, requests
Required-by: 
Note: you may need to restart the kernel to use updated packages.

Let us see if we can use tweepy now.

In [3]:
import tweepy
In [5]:
auth = tweepy.OAuth1UserHandler(
   consumer_key, consumer_secret, access_token, access_token_secret
)

api = tweepy.API(auth)

public_tweets = api.home_timeline()
for tweet in public_tweets:
    print(tweet.text)
    break
“RIP to another pandemic perk for junior bankers,” one junior Goldman banker lamented. “I’m sure the partners still… https://t.co/Xun47Y7ovb

Ok so far so good, Let us try the api.search() method now.

In [6]:
for tweets in api.search(q="aapl", lang="en"):
    print(tweets)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/tmp/ipykernel_6135/3050917875.py in <module>
----> 1 for tweets in api.search(q="aapl", lang="en"):
      2     print(tweets)

AttributeError: 'API' object has no attribute 'search'

In tweepy 4.0.0 and above versions, api.search has been replaced with api.search_tweets()

In [11]:
tweets = api.search_tweets(q="aapl", lang="en")
In [17]:
len(tweets)
Out[17]:
15