How to Set Up Travis CI with Github for a Python Project

This post is part of a series. This list contains all of the posts:

Travis CI Tutorial for a Python Project

Travis CI is a free and highly popular continuous integration system that hooks into Github repositories. It's very simple to connect, and every time you push to Github, Travis will create a small environment, run unit tests, and indicate if it passed or failed. You can even put a badge on your readme for all to see.

How to set up Travis

Sign up with your Github account at

From the left you can click on the + icon next to My Repositories to add a new Github repo:


Find the repo you want to add and check it:


Click on the repo name, and then switch into your IDE.

I prefer to use the following directory structure, where /blog is project root.


First create a .travis.yml file in project root and add the following basic structure to it:

language: python
 - "2.7"
install: pip install -r requirements.txt
script: sh

Travis will run your application inside of virtualenv, so the install portion is required and must point to a file containing the pip packages you need installed.

The script portion will run the command that kicks off the tests you want to run. I like to use a bash file for this so I can kick up the web server and run my system tests, which rely on the web server being up and running. If you just use pure unit tests with no dependencies on a running server, you can simply use script: nosetests.

Depending on how advanced your program is, there are lots more you could add. For example, if you use postgresql, you should add

 - postgresql

If you are on Heroku or otherwise need to set environment variables:

 - BLOG_DATABASE=sqlite:///db.db

Check out all of the .travis.yml options over at Travis's Python page.

Create a requirements.txt file in project root containing all of your dependencies. Make sure to include nose in there. For example:


I like to create a file in project root with the following:

#!/usr/bin/env bash
python > /dev/null &
nosetests --with-coverage

The & will kick off in the background and then allow the tests to start. Again, I only need this because I use system tests which depend on the web server being live. is simply:

from blog import app, db
if __name__ == '__main__':
    db.create_all()'', port=5000, debug=True)

After you have these files, add them with git and push to your repo. Keep your Travis page up during this time, and it will start to refresh and print out messages.


If everything is successful, you'll see this:


Adding the Travis Badge to your GitHub Readme

At the top of your Travis page, you should see an icon for your badge that says "build passing":


Click on the icon, and change the Image URL drop down to "Markdown":


Copy and paste it at the top of your file:


Commit your changes and push to GitHub. Load up your repo's page and you can now see your badge:


This post is part of a series. This list contains all of the posts:


Add Comment




Are you human? - two = 3

Name: saviour

Creation Date: 2017-12-04

Thanks for the tutorial. I have been able to set up my first successful build.