DEV Community

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

Posted on • Originally published at modelpredict.com

2 2

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

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

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay