Python

This guide covers configuring Python projects on Semaphore. If you’re new to Semaphore please read our Guided tour first.

Supported Python versions

Semaphore provides major Python versions and tools preinstalled. You can find information about them in the Ubuntu image reference.

Python 2.7 is the default version. This can be switched to 3.7 with sem-version. Here's an example:

blocks:
  - name: Tests
    task:
      prologue:
        commands:
          - sem-version python 3.7
      jobs:
        - name: Tests
          commands:
            - python --version

Dependency caching

Caching Pip Downloads

You can use Semaphore's cache command to store and load a pip download cache directory. This does not cache the installations, just the downloads but it will speed up builds significantly. Here's an example:

blocks:
  - name: Tests
    task:
      prologue:
        commands:
          # Add --local pip bin to $PATH
          - export PATH=$HOME/.local/bin:$PATH
          - checkout
          - mkdir .pip_cache
          - cache restore pip-$SEMAPHORE_GIT_BRANCH-$(checksum requirements.txt),pip-$SEMAPHORE_GIT_BRANCH,pip-master
          # Use --user to avoid permission conflicts
          - pip install --user --cache-dir .pip_cache -r requirements.txt
          - cache store pip-$SEMAPHORE_GIT_BRANCH-$(checksum requirements.txt) .pip_cache
      jobs:
        - name: Everything
          commands:
            - py.test

Pipenv

You can use Semaphore's cache command to store and load pipenv's virtualenv. This requires setting the PIPENV_VENV_IN_PROJECT environment variable. In the following configuration example, we install dependencies and warm the cache in the first block, then use the cache in subsequent blocks.

version: v1.0
 name: Python Example
 agent:
   machine:
     type: e1-standard-2
     os_image: ubuntu1804

 blocks:
   - name: Install dependencies
     task:
       env_vars:
         - name: PIPENV_VENV_IN_PROJECT
           value: "true"
       prologue:
         commands:
           - sudo pip install pipenv
           - checkout
       jobs:
         - name: Install and cache dependencies
           commands:
             - cache restore pipenv-$SEMAPHORE_GIT_BRANCH-$(checksum Pipfile.lock),pipenv-$SEMAPHORE_GIT_BRANCH,pipenv-master
             # --deploy also checks python version requirements
             - pipenv install --dev --deploy
             - cache store pipenv-$SEMAPHORE_GIT_BRANCH-$(checksum Pipfile.lock) .venv
   - name: Tests
     task:
       env_vars:
         - name: PIPENV_VENV_IN_PROJECT
           value: "true"
       prologue:
         commands:
           - sudo pip install pipenv
           - checkout
           - cache restore cache restore pipenv-$SEMAPHORE_GIT_BRANCH-$(checksum Pipfile.lock),pipenv-$SEMAPHORE_GIT_BRANCH,pipenv-master
       jobs:
         - name: Everything
           commands:
             # assuming you have "test" in your Pipfile scripts
             - pipenv run test

If you need to clear cache for your project, launch a debug session and execute cache clear or cache delete <key>.

Environment Variables

Semaphore doesn't set project specific environment variables like TESTING used in Flask. You can set these at the task level.

blocks:
  - name: Tests
    task:
      env_vars:
        - name: TESTING
          value: "1"
      jobs:
        - name: Everything
          commands:
            - python test.py

C-Extensions & system dependencies

Projects may need system packages in order to install pips like postgres. Semaphore provides full sudo access so you may install all required packages. Here's an example of installing the postgres pip:

blocks:
  - name: Tests
    task:
      prologue:
        commands:
          - sudo apt-get update && sudo apt-get install -y libpq-dev
          - pip install postgres
      jobs:
        - name: Everything
          commands:
            - python test.py

Still need help? Contact Us Contact Us