Poetry doesn't use the correct version of Python

34,594

Solution 1

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.

Solution 2

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.

Solution 3

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.

Share:
34,594

Related videos on Youtube

P4nd4b0b3r1n0
Author by

P4nd4b0b3r1n0

Updated on July 09, 2022

Comments

  • P4nd4b0b3r1n0
    P4nd4b0b3r1n0 almost 2 years

    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.

    • a_guest
      a_guest about 4 years
      Does this issue help you / clarify the status?
  • Chris
    Chris about 4 years
    "Poetry should manage all your python dependencies so that there is no need to use pyenv."—pyenv isn't for dependencies. It manages versions of Python itself. Does Poetry do that natively?
  • Sam Broster
    Sam Broster about 4 years
    Ah my mistake. The keypoint is that the pyproject.toml file is saying that your project supports python2.7 or great, it is not saying 'I want to use python3.8'. If you want to update the versions of python your project supports then update toml file directly
  • Sam Broster
    Sam Broster about 4 years
    I've updated my answer, hopefully it makes more sense now.
  • P4nd4b0b3r1n0
    P4nd4b0b3r1n0 about 4 years
    Great answer, thanks. Although I do have a follow-up question: How do I increase the number of virtual environment options for Poetry? When I run poetry env list now, there's only one option and that's for Python 2.7. What I'd like then is to also have and option for Python 3.8 for example.
  • Sam Broster
    Sam Broster about 4 years
    I think you want something like: poetry use python3.8 to switch to 3.8, and then poetry install to create the virtual env for 3.8
  • Matthew Schinckel
    Matthew Schinckel about 3 years
    Interestingly, I needed to do this, even though I had a .python-version with the correct version in the directory.
  • blong
    blong about 3 years
    I have no idea if the command changed over the last year, but I think the current command is poetry env use python3.8 . Source: web.archive.org/web/20210130151158/https://python-poetry.org‌​/…
  • juanmirocks
    juanmirocks over 2 years
    If the .python-version is not being read, you might need to fix your pyenv configuration: github.com/pyenv/pyenv#basic-github-checkout
  • Joe Sadoski
    Joe Sadoski about 2 years
    This is the solution to most of the problems I have. (And embarrassingly, I need to google this answer every time I do this!) Thank you.