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
3
Answers
The root cause is the section
with the line caching
poetry
. Sincepoetry
was previously installed with apip
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:
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 thesetup-python
step.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: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 inactions/setup-python
.Fix
Cheat
You can do one of two things. The simplest is a cheat:
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 beforepoetry install
: