Github action using wrong version of Python

Viewed 129

I have the following Github action, in which I'm specifying Python 3.10:

name: Unit Tests
runs-on: ubuntu-latest
defaults:
  run:
    shell: bash
    working-directory: app
steps:
  - uses: actions/checkout@v3
  - name: Install poetry
    run: pipx install poetry
  - uses: actions/setup-python@v3
    with:
      python-version: "3.10"
      cache: "poetry"
  - run: poetry install
  - name: Run tests
    run: |
      make mypy
      make test

The pyproject.toml specifies Python 3.10 as well:

[tool.poetry.dependencies]
python = ">=3.10,<3.11"

When the action runs, I get the following:

The currently activated Python version 3.8.10 is not supported by the project 
(>=3.10,<3.11).
Trying to find and use a compatible version. 
Using python3 (3.10.5)

It would look like it's using 3.10, but py.test is using 3.8.10:

platform linux -- Python 3.8.10, pytest-6.2.5, py-1.11.0, pluggy-1.0.0 -- 
/home/runner/.cache/pypoetry/virtualenvs/vital-background-pull-yluVa_Vi-py3.10/bin/python

For context, this Github action was running on 3.8 before. I've updated the python version in both the test.yaml and the pyproject.toml but it's still using 3.8. Anything else I should change to make it use 3.10?

Thank you

2 Answers

The root cause is the section

- uses: actions/setup-python@v3
  with:
    python-version: "3.10"
    cache: "poetry"

with the line caching poetry. Since poetry was previously installed with a pip associated with Python 3.8, the package will be retrieved from the cache associated with that Python version. It needs to be re-installed with the new Python version.

You can either remove the cache: poetry from a single GH actions execution, or remove the cache manually. This will fix your issue.

Pipx might install poetry using an unexpected version of python. You can specify the python version to use:

pipx install poetry --python $(which python)
# or
pipx install poetry --python python3.10

This is what I do after the setup-python@v3 step.

You could also specify the path to the expected python version, those are available in the github docs. This would allow you to do the cache: poetry step in the order you have above.

I had a similar problem and solved it after reading how does pipx know which python to use. I did not use pyenv in my case, since I'm specifying version in my setup-python@v3.

You might also install poetry after the python setup step to be sure your version is available, supposing you did python-version: "3.10.12" or something. Then what remains is cacheing, perhaps using the cache action separately from the setup-python step.

Related