Search code examples
pythontwitter

Python Tweepy api: create_friendship() takes 1 positional argument but 2 were given


I want to use create_friendship of tweepy to follow specific accounts. But i keep getting the error.

Error

  File "twitter_retweet_by_word.py", line 20, in <module>
    api.create_friendship("**********")
  File "/Users/*****/opt/anaconda3/lib/python3.8/site-packages/tweepy/api.py", line 46, in wrapper
    return method(*args, **kwargs)
TypeError: create_friendship() takes 1 positional argument but 2 were given

I have checked the similar problem in the link below but I did't understand what I had to do to make it work.

Tweepy api: create_friendship() takes 1 positional argument but 2 were given

My code

import tweepy

consumer_key = ""
consumer_secret = ""
access_token = ""
access_token_secret = ""

# OAuth
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

api = tweepy.API(auth)




for status in api.search_tweets(q='Python RT', count=2):
    tweet_id = status.id
    print(tweet_id)
    user_id = status.user.screen_name
    print(user_id)
    api.create_friendship(user_id)

Could you please tell me how exactly I need to change the code to make it work? Thanks in advance.

python version: 3.8.5 tweepy Version: 4.3.0


Solution

  • From what I understand, you have to call the function as such:

    api.create_friendship(user_id=user_id)
    

    create_friendship(self, kwargs**) doesn't have any other implicit arguments other than self, which is never used when calling the function.

    create_friendship(user_id=some_variable) tells the function which argument you are giving and now it knows the only argument it can use inside itself is the user_id