DEV Community

loading...
Cover image for Github Actions: using python version from .python-version file (pyenv)

Github Actions: using python version from .python-version file (pyenv)

Mario Kostelac
Building ML-based products at intercom.io! Constantly torn between ML and Web.
Originally published at modelpredict.com ・1 min read

I was recently creating a CI pipeline for a toy ML project. It was to make sure that our training accuracies stay within some thresholds as we change the code. We were hosting the project on GitHub so GitHub Actions seemed like a great fit to run our CI.

I’d found GitHub’s actions/setup-python@v2 to set up the specific python version, but it wasn’t clear how to set a version used pyenv (a great tool for managing multiple python versions btw), one written in the .python-version file.

Turns out combining action contexts and environment variables does the trick.

  1. One step reads the .python-version file and writes that to environment variable
  2. actions/setup-python@v2 installs the version from the environment variable.

Here's the snippet defining the job steps

 steps:
    - uses: actions/checkout@v2
    - name: Get python version
      run: |
        python_version=$(cat .python-version)
        echo "python_version=${python_version}" >> $GITHUB_ENV
    - name: Set up Python ${{ env.python_version }}
      uses: actions/setup-python@v2
      with:
        python-version: ${{ env.python_version }}
Enter fullscreen mode Exit fullscreen mode

Discussion (0)