Get up and running with Python and Travis CI
Tags: devops, python, opensource
Published: 2020-10-19 05:32:07

---
title: Get up and running with Python and Travis CI
published: true
description: How to set up Rails with Travis CI
tags: devops,python,opensource
//cover_image: ![Python Cookbook](https://dev-to-uploads.s3.amazonaws.com/i/kz09qm0imuzxs9hyhadl.png)
---

Python is one of the most versatile languages in the programming world - it's not only one of the fist languages new programmers get introduced to either through education or hobby projects, but it's also a tried and trusted go-to for experienced programmers in engineering, science, mathematics and commercial software development. Created way back in 1991, Python is one of the most enduring langauages in the programming community and continues to receive regular updates to keep it as relevant today as it was back then.

Needless to say, most of the Travis CI team are also pretty fond of it - let's get stuck into the Python Cookbook!

# Let's do some Python! 
Okay, let's do this! Open up your favorite text editor or IDE.  A classic `.travis.yml` file for Python would look like this: 

```yaml
dist: xenial

language: python

cache: pip

python:
    - "3.6"
    - "3.7"
    - "3.8"
    - "nightly"

matrix:
    allow_failures:
        - python: "nightly"

install:
    - pip install pipenv --upgrade-strategy=only-if-needed
    - pipenv install --dev

script:
    - bash scripts/test.sh

after_script:
    - bash <(curl -s https://codecov.io/bash)
 ```
 
 Next were going to explain what `dist` is, and why you need it! 
 
 ```yaml
 dist: xenial
 ```
 
 `dist` in the .yml file is where the Ubuntu Release codename is specified.  Please see: [releases](http://releases.ubuntu.com/) for a full list. This specifies the base operating system used for the rest of the workflow.
 
 Now we are gonna talk about caching, we all like speed right? It starts out, something like this:
 
 ```yaml
 cache: pip
 ```
 
Cache allows for a Python package versions to be stored between runtime, to speed up sequential builds. Cache can apply to more than just Python packages.

## Python versions

```yaml
python:
    - "3.6"
    - "3.7"
    - "3.8"
    - "nightly"
  ```
    
Python, given the above language specification, is a key for a sequence of Python versions to perform builds against. Generally most CI tools use the latest bug release version for each minor version. The build logs will tell you the specific versions, you can also view these logs manually via running in debug mode. 

## Matrix

```yaml
matrix:
    allow_failures:
        - python: "nightly"
   ```
`Matrix` allows for modifications in the above build sequence. In this case, the `allow_failures` key specifies a reference to the Python sequence above, and has the value of "nightly", meaning that that version is _possibly_ allowed to fail. Depending on what more build instructions you have.

At some point, you'll have a requirements.txt file, in here this is where you'll have your dependencies: 

```yaml
pip install -r requirements.txt
```

To continue with the Python build, you're going to need to run some updates in the `.travis.yml` file, via: 

```yaml
before_install:
  - sudo apt-get update
  - sudo apt-get install python3-pip
  - sudo apt-get install python3-pytest
```

Now add an existing project to GitHub, let's invoke the classic GitHub build flow (init a repo, push files). Head on over to Travis CI, login, sync up and start the build for your Python project! If you forgot the build flow luckily we've attached it below! Open a terminal, run these commands and you can then push your Python project to GitHub via:

```bash
git init
git add . 
git commit -m "Travis build" 
git remote add origin remote repository URL
git remote -v 
git push -u origin master
```

There you go, your Python project is now connected to Travis, and your CI/CD is ready to go! 
 
## Cookbook Series

We have new recipes every other week, make sure you come back for a practical way of using Travis for beginners. 

Author: Montana Mendy

Originally posted on the [Travis CI blog](https://blog.travis-ci.com/rails-cookbook)