3

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

Blackecho
  • 1,226
  • 5
  • 18
  • 26

3 Answers3

3

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.

Matthew Schuchard
  • 25,172
  • 3
  • 47
  • 67
  • I don't think this is it. I set up the same thing with a new workflow, there's no cache, I am using `cache-dependency-path` explicitly, and you have to install poetry before setup-python or setup-python will fail because it calls `poetry env use`. – Mike Gillis Oct 01 '22 at 20:54
  • Yes, that's it. Turns out it was the cache not being properly refreshed. I manually removed it and that solved it. Thanks! – Blackecho Oct 04 '22 at 09:27
0

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.

Merlin
  • 1,780
  • 1
  • 18
  • 20
0

In my case, this happens because my pyproject.toml is in a subdirectory of the repository.

The log for my actions/setup-python@v4 action looks like this:

/opt/pipx_bin/poetry env use /opt/hostedtoolcache/Python/3.9.14/x64/bin/python

Poetry could not find a pyproject.toml file in /home/runner/work/PLAT/PLAT or its parents
Warning: 
Poetry could not find a pyproject.toml file in /home/runner/work/PLAT/PLAT or its parents

But the action completes successfully. Later, poetry doesn't know what python to use because it was unable to write to its global envs.toml. Eventually I did find that there's an open issue for this in actions/setup-python.

Fix

Cheat

You can do one of two things. The simplest is a cheat:

runs-on: ubuntu-22.04

The ubuntu-22.04 image has Python 3.10 baked in, so you can just forget about switching pythons and that'll be ok for a while.

Actual Fix

The better fix is to add a step after setup-python but before poetry install:

   - run: poetry env use ${pythonLocation}/bin/python
     working-directory: wherever/your/pyproject.toml/is