Skip to content

Latest commit

 

History

History
104 lines (69 loc) · 2.59 KB

File metadata and controls

104 lines (69 loc) · 2.59 KB
description Python Guide
sidebar_position 4

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 {#switch}

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

sem-version python 3.12

Using Docker containers {#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 {#caching}

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 {#test-results}

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
- 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