Building a Python Project
What This Guide Covers
Language versions and other build-environment specific information are in our reference pages:
Python builds are not available on the OS X environment.
Specifying Python versions
Specify python versions using the
python key. As we update the Python build
images, aliases like
3.6 will point to different exact versions or patch
language: python python: - "2.6" - "2.7" - "3.3" - "3.4" - "3.5" - "3.5-dev" # 3.5 development branch - "3.6" - "3.6-dev" # 3.6 development branch - "3.7-dev" # 3.7 development branch - "nightly" # command to install dependencies install: - pip install -r requirements.txt # command to run tests script: - pytest # or py.test for Python versions 3.5 and below
Travis CI Uses Isolated virtualenvs
The CI Environment uses separate virtualenv instances for each Python
version. This means that as soon as you specify
language: python in
.travis.yml your tests will run inside a virtualenv (without you having to explicitly create it).
System Python is not used and should not be relied on. If you need
to install Python packages, do it via pip and not apt.
If you decide to use apt anyway, note that for compatibility reasons, you’ll only be able to use the default Python versions that are available in Ubuntu (e.g. for Trusty, this means 2.7.6 and 3.4.3).
To access the packages inside the virtualenv, you will need to specify that it should be created with the
To do this, include the following in your
language: python virtualenv: system_site_packages: true
Travis CI supports PyPy and PyPy3.
To test your project against PyPy, add “pypy” or “pypy3” to the list of Pythons
language: python python: - "2.6" - "2.7" - "3.4" - "3.5" - "3.6" # PyPy versions - "pypy" # PyPy2 5.8.0 - "pypy3" # Pypy3 5.8.0-beta0 # command to install dependencies install: - pip install . - pip install -r requirements.txt # command to run tests script: pytest
Nightly build support
Travis CI supports a special version name
nightly, which points to
a recent development version of CPython build.
Development releases support
From Python 3.5, Python In Development versions are available.
You can specify these in your builds with
Default Build Script
Python projects need to provide the
script key in their
specify what command to run tests with.
For example, if your project uses pytest:
# command to run tests script: pytest # or py.test for Python versions 3.5 and below
if it uses
make test instead:
script: make test
If you do not provide a
script key in a Python project, Travis CI prints a
message (“Please override the script: key in your .travis.yml to run tests.”)
and fails the build.
By default Travis CI uses
pip to manage Python dependencies. If you have a
requirements.txt file, Travis CI runs
pip install -r requirements.txt
install phase of the build.
You can manually override this default
install phase, for example:
install: pip install --user -r requirements.txt
Please note that the
--user option is mandatory if you are not using
language: python, since no virtualenv will be created in that case.
Custom Dependency Management
To override the default
pip dependency management, alter the
step as described in general build
Testing Against Multiple Versions of Dependencies (e.g. Django or Flask)
If you need to test against multiple versions of, say, Django, you can instruct Travis CI to do multiple runs with different sets or values of environment variables.
Use env key in your .travis.yml file, for example:
env: - DJANGO_VERSION=1.10.8 - DJANGO_VERSION=1.11.5
and then use ENV variable values in your dependencies installation scripts, test cases or test script parameter values. Here we use ENV variable value to instruct pip to install an exact version:
install: - pip install -q Django==$DJANGO_VERSION - python setup.py -q install
The same technique is often used to test projects against multiple databases and so on.
For Python projects,
python can be given as arrays
to construct a build matrix.