Skip to main content

Python

This guide will help build Python projects on Semaphore.

Overview

The Python interpreter is pre-installed in the Linux and macOS Semaphore environments. You can switch the active interpreter using sem-version. You may also use Docker images.

How to select Python versions

Change the active Python versions on Linux with sem-version.

sem-version python 3.12

Using Docker containers

The sem-version tool does not work on Docker containers. You must use a pre-built Docker image with the language versions you need and run the job using Docker environments.

You can use the pre-build Python images or build your own. Find Dockerfiles to build your custom images in the semaphoreci/docker-images repository.

How to cache packages

To cache downloaded Python packages, you must download them to the .pip_cache/ folder relative to the current directory.

So, the first job on the pipeline should contain the following commands:

export PATH=$HOME/.local/bin:$PATH
checkout
mkdir .pip_cache
cache restore
pip install --user --cache-dir .pip_cache -r requirements.txt
cache store

How to set up test reports

This section explains how to set up test reports (and flaky tests) for Python and pytest.

  1. Set the name of your test suite in the pytest config file

    [pytest]
    junit_suite_name = my_suite
  2. Run your tests with the --junitxml argument

    pytest --junitxml=junit.xml
  3. Create an after_pipeline job with the following command:

    test-results publish junit.xml
Example pipeline definition
Using test reports on Python
- name: Tests
task:
prologue:
commands:
- export PATH=$HOME/.local/bin:$PATH
- checkout
- mkdir .pip_cache
- cache restore
- pip install --user --cache-dir .pip_cache -r requirements.txt
- cache store

job:
name: "Tests"
commands:
- pytest --junitxml=junit.xml tests/*.py

epilogue:
always:
commands:
- test-results publish junit.xml