Visual Studio Code – Connect to Twitter with Python

Visual Studio Code is a cross platform editor that supports multiple programming languages. Combining with Python, and its associated 3rd party packages that wrap Twitter’s API, we can easy connect to Twitter, read and use the data in just few lines of code.

Why Twitter ?

Twitter is a popular social network where users can share short SMS-like messages called tweets. Users share thoughts, links and pictures on Twitter, journalists comment on live events. The list of different ways to use Twitter could be really long, and with 500 millions of tweets per day, there’s a lot of data to play with.

Using an excellent Python library – TwitterAPI as a minimal wrapper to native Twitter API calls, we can have a tool to extract the information we need.

Getting started

At first, you will need to create a Twitter account and then a Twitter application. Once you have an account, you’ll have to go to their developer’s site, in the Application Management area and create a new application. After your application is created, you will need to get your API keys (or generate some) and also generate access tokens.

Once you first open the VS Code, open the Extensions list and install Python extension.

Download from GitHub the code (click to download) and extract the archive to a local folder. Then use the option File -> Open Folder in VSCode.

To install the Twitter library, open the command prompt and run each of the next install commands. You could also run these lines within VSCode. It has integrated the command prompt, and you can easy access it by clicking on the menu item: View -> Integrated terminal.

pip install TwitterAPI
pip install configparser

Connecting to Twitter

To be able to do this we will use the TwitterAPI library. With credentials from Twitter, we can access the Twitter data:

Debug the code with VS Code

We can run the above code and easily debug in VS Code. To be easier, dowload the code and open the VS Code debugger, selecting Python as language.

Python plugin for VSCode comes with linting support, which is source code, bug and quality checker, following the style recommended by Python style guide (PEP-8). Pylint quickly highlights the errors and it shows recommendations on how to write the code.

Or run directly

The other quick way to run the code, without debug, would be to use the combination CTRL-B. This uses the configuration file task.json, saved in folder .vscode and run automatically the code file using Python.