2023-03-03

Setting Up Python 3.10 in GitHub Actions

Introduction

In this article, I will show you the steps required to set up Python 3.10 in GitHub Actions, issues that will be encountered, and methods to circumvent those.

Setting Up Python 3.10 in GitHub Actions

GitHub Actions use workflows to automate processes, which are defined in YAML files. To use Python 3.10 in your workflow, you will need to specify it in your workflow file. The file is typically stored in the .github/workflows/ directory of your repository. Here, we will name our workflow file setup-python310.yml.

The code to set up Python 3.10 in your GitHub Action workflow should look something like this:

.github/workflows/setup-python310.yml
name: Setup Python 3.10

on:
  workflow_dispatch:

jobs:
  setup_python_310:

    runs-on: ubuntu-20.04

    steps:
      - name: Setup Python
        uses: actions/setup-python@v4
        with:
          python-version: 3.10

      - name: Display Python version
        run: python -c "import sys; print(sys.version)"

In the above workflow, the job setup_python_310 runs on an Ubuntu 20.04 virtual environment. It consists of two steps: firstly, setting up Python with the desired version (3.10 in this case), and secondly, displaying the version of Python to confirm the setup. This workflow is triggered manually via the workflow_dispatch event.

But the above setup does not work as expected due to an issue with how YAML interprets numbers.

Version Not Found Error

Despite setting up Python 3.10 correctly in our workflow file, the execution fails, yielding an error stating that Python version 3.1 is not found in the local cache:

Version 3.1 was not found in the local cache
Error: The version '3.1' with architecture 'x64' was not found for Ubuntu 20.04.
The list of all available versions can be found here: https://raw.githubusercontent.com/actions/python-versions/main/versions-manifest.json

The problem lies within the YAML language itself. In YAML, floating-point numbers like 3.10 are treated as decimal numbers and trailing zeroes are removed, leading to 3.10 being interpreted as 3.1.

Solution: Correctly Formatting Python Version

To prevent YAML from truncating the trailing zero, we simply need to quote the version number. This forces YAML to treat it as a string rather than a floating-point number:

.github/workflows/setup-python310.yml
  name: Setup Python 3.10

  on:
    workflow_dispatch:

  jobs:
    setup_python_310:

      runs-on: ubuntu-20.04

      steps:
        - name: Setup Python
          uses: actions/setup-python@v4
          with:
-           python-version: 3.10
+           python-version: '3.10'

        - name: Display Python version
          run: python -c "import sys; print(sys.version)"

By making this small change, our Python version is correctly recognized and the setup proceeds as expected.

Ryusei Kakujo

researchgatelinkedingithub

Focusing on data science for mobility

Bench Press 100kg!