Last Updated: Sunday 24th March 2013

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:

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.

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:

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.

Running the Twython Script

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

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.

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

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.

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:

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.

  • http://www.computerkorner.org/ Gray Code

    ahaann.. now that was what i was looking for.. pretty interesting.. wish there was something like fbython for Facebook.. haha

  • http://venodesigns.net/ Ryan McGrath

    I’m the original author of Twython – just wanted to drop by and say thanks for writing about it!

    • http://lostinthegc.wordpress.com Sergio Tapia

      Thanks! It really is a fantastic library that makes things really simple for Python devs out there.

  • genry

    you need space in fromtwython

    • Joanna Alonzo

      Hi, genry! Thanks for pointing that out. Changes were made to the article.

  • valentina

    hello! really useful library!
    There are some errors in the sintax of the article… you need space between print twitter and print trends!
    thanks anyway for helping me!

  • Pingback: Introduction to tweepy, Twitter for Python - Python Central

  • http://www.facebook.com/purushoth.m Purushoth Mahendran

    I have been trying to do Twitter Search using Twython. However when I am running the script I am getting “TwythonError : Bad Request” do we need to perform authentication for Searching. I guess the Search API doesn’t need any authentication . Can someone help me fix this !!