Can someone explain to me what is going on here?
I’m trying to get pyenv and poetry to place nice together. I am on an AWS instance of Ubuntu 20.04
which has python 3.8.10
installed. (I have removed all traces of python2 from the system). I would like to use python 3.10 but I can’t just upgrade to that (thank you very much Amazon). So enter pyenv
.
I made an empty project with the poetry new
command and here is the pyproject.toml
file.
[tool.poetry]
name = "test"
version = "0.1.0"
description = ""
authors = ["ken <[email protected]>"]
readme = "README.md"
[tool.poetry.dependencies]
python = "^3.10"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
I have 3.10.7 installed through pyenv. If I run poetry run python --version
I get the following output.
The currently activated Python version 3.8.10 is not supported by the project (^3.10).
Trying to find and use a compatible version.
Using python3 (3.10.7)
Python 3.8.10
It finds and "uses" 3.10.7 but then reports 3.8.10? Huh?
If I then run poetry env use 3.10
and try again I get …
Current Python version (3.8.10) is not allowed by the project (^3.10).
Please change python executable via the "env use" command.
… and it fails to run completely, i.e. no version reported from the python command. How is my current python version still 3.8.10. If I run python --version
at the command-line straight away (not through poetry), I get 3.10.7
. What is going on here?!
As a check if I run poetry env use system
then I indeed get back to my first problem. 🙁
2
Answers
Poetry can’t handle the Python dependency for you. That is, it can’t install the correct Python version for you; it can only handle Python package dependencies correctly for you. But it will check for the correct Python version.
Since Poetry itself depends on Python, and in this case that’s an older version (3.8) than required (3.10), it likely gets confused, and can’t work with this repository: Poetry doesn’t know about your Python 3.10! (or at least, avoids making guesses.) The error message could be clearer (for example, "Poetry is installed with Python version 3.8, but this project requires 3.10. Please install Poetry for a working version 3.10"), but the error does hint at this problem.
Install Poetry for the correct Python version (Pyenv 3.10 here), and use that Poetry.
Note: when you start a project with
poetry new
orpoetry init
, Poetry will default to its default Python as main dependency. So initially, your Python dependency in thepyproject.toml
file will have been 3.8. You then probably changed that yourself to 3.10. That is okay-ish, but Poetry didn’t know about that (in fact, it can’t change the Python dependency version). If you had started directly from Pyenv’s 3.10 with, say,python -m poetry init
, you would have been fine.Additional remark: numerous issues on Poetry’s GitHub suggest that this can work in ways (there are various issues of people running into this error message and using Pyenv, but responses suggest this can work), so try a search there.
In fact, for testing, it makes a lot of sense to be able to test this with, in this case, Python 3.10 and 3.11. While Poetry would only be installed for 3.10, 3.11 should be to work as well. At a guess here, the difference is that you are developing with 3.11 (and thus using Poetry v-env), but only testing with 3.11, running some variant of
python3.11 -m pip install .
for that test. That would first download Poetry for 3.11, then build the rest of the project, and run the tests, in a temporary directory. Which is a different thing than what you are doing currently (given the question).You have python 3.10 install when is great, but the system version of python still exist. What you have to do now is to switch the python version you have just installed.
you can run
pyenv global 3.10
this will switch to the 3.10 version you just installedAfter that you can run your poetry command and it will work since your
pyproject.toml
specifies that you need to use the python3.10In the future if you need to use another version of python install it with
pyenv install 3.x
and the switch to it withpyenv global 3.x
You can also use
local
instead ofglobal
if you’re in the working directory and don’t want to make the version your global python version