"
This article is part of in the series
Published: Monday 3rd September 2012
Last Updated: Wednesday 29th December 2021

Python is a very versatile language with a very diverse library ecosystem. That's why their motto is "batteries included" - and boy are they right! Using Python, you can connect and interact with a wide array of social networks including Facebook, Twitter and LinkedIn.

Today, I want to show you how to perform some basic tasks on Twitter using Twython and Python.

Twython is a very robust, mature library for Twitter. It has been maintained for over 2 years. It's actively maintained by its core team and receives patches from the community very regularly. This is not an alpha-status library; it's tested and used in many commercial applications.

You can find Twython's Github page here: https://github.com/ryanmcgrath/twython. You will see the library's source code and list of contributors on their repository page. They also offer a simple .py file download for you to use.

Installing Python's Twython

Easy Install will handle fetching the latest version for you and installing it in a place where Python will be able to access it.

If you're not familiar with Easy Install, it’s an easy Python module (easy_install) bundled with setuptools that lets you automatically download, build, install, and manage Python packages. Easy Install is included in the official Python 3.x and Python 2.x distributions.

After installing Easy Install, just run the following command to have it download and package the latest version of Twython:

[shell]
easy_install twython
[/shell]

Creating a Twython Object

Let's start by importing the library and creating a Twython object. We'll be using this object for all interactions with Twitter.

[python]
from twython import Twython
twitter = Twython()
[/python]

Getting a Twitter User’s Timeline

For our first demo, we're going to perform one of the most common features a developer would want from Twitter: getting a user's timeline. As an example, I'll search for my own timeline. Here’s how:

[python]
from twython import Twython
twitter = Twython()
# First, let's grab a user's timeline. Use the
# 'screen_name' parameter with a Twitter user name.
user_timeline = twitter.getUserTimeline(screen_name="pythoncentral")
[/python]

Print User Tweets in Twython

Now that we have a user's timeline, we can use Twython to display the individual Tweets. Since the user_timeline variable is just a collection you can skip or take the amount of Tweets you need.

[python]
# And print the tweets for that user.
for tweet in user_timeline:
print(tweet['text'])
[/python]

Running the Twython Script

Save your script (in my case, it's named twythonExample.py), and run it using the Python command:

[shell]
python twythonExample.py
[/shell]

You should see some tweets coming up on the console. If you don't, double check that the Twython library is installed correctly in the Libs folder of your Python installation.

The code itself is self explanatory; each tweet has a wealth of data inside of it. In this case, we're reaching in and printing the "text" element.

Use Twython, make Twitter interaction easier!

More Twython Stuff to Try Out

Fetching a Gravatar Image

Now let's try fetching a user’s Gravatar image.

[python]
from twython import Twython
twitter = Twython()
[/python]

Grabbing a user’ s Gravatar icon is very simple. You can also determine what size of gravatar you want.

[python]
print(twitter.getProfileImageUrl('pythoncentral', size='bigger'))
print(twitter.getProfileImageUrl('pythoncentral'))
[/python]

As you can see, we can request for a specific size of gravatar, or if you just want the default size not request a size at all.

Searching Twitter with Twython

How about some Twitter searching? Twython makes this a trivial issue.

[python]
from twython import Twython
twitter = Twython()
# Some basic search functionality.
print(twitter.search(q='pythoncentral'))
[/python]

You should see a result set of Tweets with the query you wrote coming up on the console. Best of all, the process is really fast with minimal overhead during the search.

Fetching Daily and Weekly Twitter Trends

Lastly, Twython offers a way to fetch Daily and Weekly trends. Both equally simple to invoke:

[python]
from twython import Twython
twitter = Twython()
# Displaying the Daily Trends.
trends = twitter.getDailyTrends()
print(trends)
# Displaying the Weekly Trends.
weeklyTrends = twitter.getWeeklyTrends()
printweeklyTrends
[/python]

Twython is a very powerful library with a great team of contributors that have been working on it for a long time. I would highly recommend this library if you have any Twitter needs within Python.