Poetry doesn't use the correct version of Python

Question:

I’ve recently installed both Pyenv and Poetry and want to create a new Python 3.8 project. I’ve set both the global and local versions of python to 3.8.1 using the appropriate Pyenv commands (pyenv global 3.8.1 for example). When I run pyenv version in my terminal the output is 3.8.1. as expected.

Now, the problem is that when I create a new python project with Poetry (poetry new my-project), the generated pyproject.toml file creates a project with python 2.7:

[tool.poetry]
name = "my-project"
version = "0.1.0"
description = ""
authors = ["user <[email protected]>"]

[tool.poetry.dependencies]
python = "^2.7"

[tool.poetry.dev-dependencies]
pytest = "^4.6"

[build-system]
requires = ["poetry>=0.12"]
build-backend = "poetry.masonry.api"

It seems that Poetry defaults back to the system version of Python. How do I change this so that it uses the version installed with Pyenv?

Edit

I’m using MacOS, which comes bundled with Python 2.7. I think that might be causing some of the issues here. I’ve reinstalled Python 3.8 again with Pyenv, but when I hit Poetry install I get the following error:

The currently activated Python version 2.7.16 is not supported by the project (^3.8).
Trying to find and use a compatible version.

[NoCompatiblePythonVersionFound]
Poetry was unable to find a compatible version. If you have one, you can explicitly use it via the "env use" command. 

Should I create an environment explicitly for the project using Pyenv or should the project be able to access the correct Python version after running pyenv local 3.8.1.? When I do the latter, nothing changes and I still get the same errors.

Asked By: P4nd4b0b3r1n0

||

Answers:

pyproject.toml is used to define all the dependencies for your project, including the supported python version.

The line your complaining about is just saying that the versions of python supported by the project is python2.7 or greater, this is independent of what versions of python you’ve installed with pyenv.

python = "^2.7"

If you want to update the versions of python supported by the project you can edit the file directly and run poetry update.


If you want to use multiple versions of python you need to make sure poetry is using the correct dependencies for the version of python you are using. To change the specific version poetry is using you should use poetry env,

  • poetry env list show the versions of python poetry can use
  • poetry env use <python> switches poetry to use that version.

For instance on my machine poetry has 3 virtual environments installed and is using the one associated with python3.6:

↪ poetry env list
sipy-a9sqc5pb-py3.6 (Activated)
sipy-a9sqc5pb-py3.7
sipy-a9sqc5pb-py3.8

I’m not sure how these virtual environments with interact with the shivs used by pyenv but their docs have a section relating to it

Managing Virtual Environments

There is a pyenv plugin named pyenv-virtualenv which comes with various features to help pyenv users to manage virtual environments created by virtualenv or Anaconda. Because the activate script of those virtual environments are relying on mutating $PATH variable of user’s interactive shell, it will intercept pyenv’s shim style command execution hooks. We’d recommend to install pyenv-virtualenv as well if you have some plan to play with those virtual environments.

Answered By: Sam Broster

Alright, I figured the problem. A little embarrassingly, I had not run pyenv shell 3.8.1 before running any of the other commands. Everything works now. Thank you all for your efforts.

Answered By: P4nd4b0b3r1n0

Even though this issue has been resolved, I am writing this for somebody who comes across this problem again.
After all attempts my python -V always resulted in 2.7 and no discussions mentioned running pyenv shell (surprising to me!)
Adding pyenv to path

$ echo -e 'if command -v pyenv 1>/dev/null 2>&1; thenn eval "$(pyenv init -)"nfi' >> ~/.bash_profile

In my case I had to add it to .bashrc and not bash_profile.
https://ggkbase-help.berkeley.edu/how-to/install-pyenv/

Worked!

Answered By: Arpana Mehta

In my case, the environment was messed up in some way that poetry failed to activate the virtualenv properly.

Try using a different shell: perhaps, sh, or zsh. If everything works in that shell, this proves that your environment is as messed up as mine was 🙂

Use this command in both shells:

$ env

and try to spot the difference

Answered By: kolypto

You can remove the python version from pyproject.toml file and then run Poetry install

Answered By: Reza.Razi

What worked for me was to run python3.8 -m poetry install.

Answered By: Chiel

On my machine I was able to fix the "currently activated Python version is not supported by the project" error by reinstalling Poetry:

curl -sSL https://install.python-poetry.org | python3 - --uninstall
curl -sSL https://install.python-poetry.org | python3 -

After that,poetry was able to find the correct version installed by pyenv.

Answered By: Jace Browning

In my case, I had to delete and recreate the virtualenv used by poetry. This is because I added the python version restrictions (e.g. python = ">=3.6.2 <3.7") after creating the virtualenv.

Steps

  • Delete the original one: run poetry env remove myApp-XkghI9p6-py3.6
  • Run any poetry step, to create it, or run poetry shell, and confirm poetry run python --version is the correct version.
Answered By: Ben Butterworth

you can specify an explicit python executable for poetry using

poetry env use <path to python executable>

This worked for me.

Answered By: Jan

My solution to this.

First of all see the situation with this command

poetry env list

If you have an output like this: project_name-QI_LjVaV-py3.9 (Activated)

you may want to get rid of this env.

Ao you do the "deactivation" first:

deactivate

and then the "remove" after :

poetry env remove project_name-QI_LjVaV-py3.9

Now the same command:

poetry env list

should return nothing.

Then you do:

which python3

and, if the version is ok, you use this same exact output of the path of python, to tell to poetry
(Example):

poetry env use /usr/bin/python3

Do again

poetry env info 

to be sure that is using the version of the python you want.

You can continue with

poetry install
Answered By: RobyB

try re-installing poetry again

pip uninstall poetry
pip install poetry
Answered By: Paromita Sengupta