poetry: Poetry does not use active pyenv when creating virtual environment

  • I am on the latest Poetry version.

  • I have searched the issues of this repo and believe that this is not a duplicate.

  • OS version and name: MacOS 10.14 Mojave

  • Poetry version: 0.12.8

Issue

I have a project directory with a .python-version file like this:

3.7.1

Poetry has been installed into Python 3.6.5. According to recent comments, poetry is supposed to detect the active Python version when creating a new virtual environment, but it seems to stick with 3.6.5 all the time. To illustrate:

$ python --version
Python 3.7.1
$ poetry run python --version
Python 3.6.5

When I specify python = "^3.7" in `pyproject.toml I get an error:

$ poetry shell

[RuntimeError]
The current Python version (3.6.5) is not supported by the project (^3.7)
Please activate a compatible Python version.

shell

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 30
  • Comments: 53 (20 by maintainers)

Most upvoted comments

Thanks @princelySid. That would be poetry env use $(pyenv which python) for pyenv users with an active pyenv interpreter.

poetry installed with install-poetry.py to enable poetry self update. Same problem with pyenv python version.

It seems to work with explicit virtual env creation with:


poetry env use -- $(which python)

# Creating virtualenv ...-VfhLuGbT-py3.7 in .../.cache/pypoetry/virtualenvs
# Using virtualenv: .../.cache/pypoetry/virtualenvs/...-VfhLuGbT-py3.7

Versions:


poetry --version
# Poetry (version 1.2.0a1)

pyenv --version
#  pyenv 2.0.1-3-g1706436f

pyenv  versions  
#  system
# * 3.7.6 (set by PYENV_VERSION environment variable)
#   3.9.5

pyenv should pick up the .python-version. If it does not you have an issue with your configuration but this has nothing to do with Poetry.

Just in case someone end up here after hours debugging do the usual flow then poetry env use <python version>. Should work after that. The documented flow with pyenv doesn’t work any more https://github.com/python-poetry/poetry/issues/4317

Turns out that explicitly activating the correct Python version (e.g. by running pyenv shell 3.7.1) fixes this, but I was expecting this to automatically pick up what I specify in .python-version.

This is all getting pretty confusing. Perhaps the doc should spell out how to install & use poetry in the common python cases: (a1) regular python installer, (a2) regular installer + virtualenv, (b1) pyenv, and (b2) pyenv + virtualenv.

Ok, I found out that if I remove the .python-version file in my user directory (I was using this to set the default Python version), poetry is run correctly using Python 3.7.1. I’m not sure what to make of this.

At the time I am writing this (2021.06.14), I would not advise you installing poetry using install-poetry.py. Instead, stick with the old get-poetry.py. The new one doesn’t work with pyenv. It will always create the system version instead of the version pyenv specified. The old one does not have this problem.

I was having similar issues turns out it was an issue with pyenv. I followed this to resolve the steps. Specifically I had to add the following to my .bash_profile

export PATH="/Users/username/.pyenv:$PATH"
eval "$(pyenv init -)"

poetry == 1.1.14

  1. pacman -S python-poetry does not respect pyenv (local) versions
  2. pipx install poetry does not respect pyenv (local) versions
  3. only the deprecated get-poetry.py does respect pyenv (local) versions

The new install-poetry.py script does not work the same as the old one (get-poetry.py) did. The new one will install Poetry for the current Python version only.

If you need to switch between environments, we recommend using the env use command for now or, alternatively, install Poetry in each pyenv environment.

We are currently discussing ways to improve the situation so we’ll keep you posted if we ever find a better solution.

At the time I am writing this (2021.06.14), I would not advise you installing poetry using install-poetry.py. Instead, stick with the old get-poetry.py. The new one doesn’t work with pyenv. It will always create the system version instead of the version pyenv specified. The old one does not have this problem.

Seconding this as of the time of this writing.

@bersace As you can see the Python used is /home/bersace/.local/venvs/poetry/bin/python.

If you do not use the recommended installer Poetry is linked to the Python version it has been installed for. In this case I suspect you used pipx. That’s one of the shortcomings of using any pip install derived methods.

So, this is not a bug in Poetry but a limitation of the entrypoints functionality of the Python ecosystem. I’d suggest reinstalling Poetry with the recommended installer.

Maybe worth to add it here: sometimes my system Python is upgraded through system-updates, and it usually breaks my Python setup. When Poetry can’t find the right Python versions anymore, and everything else failed what I do is:

  • remove all poetry virtualenvs (rm -rf ~/.cache/pypoetry/virtualenvs/*)
  • save the list of pipx tools installed, uninstall them (including poetry, yes I install it through pipx)
  • uninstall pipx
  • uninstall pyenv (and the installed versions)

Then I can reinstall everything:

  • install pyenv
  • install Python versions through pyenv
  • make them available with pyenv global
  • install pipx with one of the pyenv versions
  • reinstall all pipx tools (including poetry)
  • recreate poetry’s virtualenvs

A bit brutal, but it also allows to clean up a bit 😄

I was having the same issue. Im using pyenv to manage python versions. I was changing my python version using pyenv local <version> and recreating my virtualenv in my IDE then running poetry install. But poetry debug:info was showing the old version. Then I deleted the .python-version file as noted above and ran pyenv shell with the new version and then poetry install and it did the trick.

The systemd file hierarchy spec (https://www.freedesktop.org/software/systemd/man/file-hierarchy.html) recommends ~/.local/bin and ~/.local/lib for this use, so I’ll second that.

@nzhwtq What motivated us to make the switch was to alleviate maintenance burden and also for security (the vendored dependencies were frozen so fixing dependencies required us to make a new release which was less than ideal).

While I understand that the convenience of having one installation of Poetry for any pyenv environment will be missed I think this is for the best.

@albireox I wouldn’t say that installing via pip is recommended even though it’s listed in the documentation (https://python-poetry.org/docs/master/#installing-with-pip) due to the fact that it can cause issues (see the warning in the documentation). However, your use case is already supported (and is the one I use myself). If you use pyenv-virtualenv and you create on virtual environment by project and the virtual environment is activated then Poetry will detect it automatically and use it. So for this use case nothing has changed.

Found the problem, the virtualenv had been created originally with the system python:

user@localhost:~/project$ poetry debug

Poetry
Version: 1.1.4
Python:  3.9.2

Virtualenv
Python:         3.5.3
Implementation: CPython
Path:           /home/user/.cache/pypoetry/virtualenvs/project-FrpO_8-J-py3.9
Valid:          True

System
Platform: linux
OS:       posix
Python:   /usr

user@localhost:~/project$ rm -rdv ~/.cache/pypoetry/virtualenvs/project-FrpO_8-J-py3.9/
OK

user@localhost:~/project$ poetry debug                                                                                                                                                                                                                                                                Poetry                                                                                                                                              Version: 1.1.4                                                                                                                                      Python:  3.9.2                                                                                                                                                                                                                                                                                          Virtualenv                                                                                                                                          Python:         3.9.2                                                                                                                               Implementation: CPython
Path:           NA

System
Platform: linux
OS:       posix
Python:   /home/user/.pyenv/versions/3.9.2

user@localhost:~/project$ poetry install
OK

user@localhost:~/project$ poetry debug

Poetry
Version: 1.1.4
Python:  3.9.2

Virtualenv
Python:         3.9.2
Implementation: CPython
Path:           /home/user/.cache/pypoetry/virtualenvs/project-FrpO_8-J-py3.9
Valid:          True

System
Platform: linux
OS:       posix
Python:   /home/user/.pyenv/versions/3.9.2

Some hints on #926 are misleading but I managed to fix this one.

Had the same issue; removing .python-version from my home directory solved it

@pawamoy Poetry is intentionally not managing python versions for you, as that would unnecessarily bloat the scope of the tool. Using pyenv in the way you’ve described above is the intended way to set things up. You can add a .python-version-file to your project, and then pyenv will automatically select the right version for you.

Possibly. Try pip uninstall poetry to see if the old Poetry installation is still there.