poetry icon indicating copy to clipboard operation
poetry copied to clipboard

`poetry env info` does not respect `virtualenvs.prefer-active-python = true` setting

Open nirvana-msu opened this issue 2 years ago • 5 comments

  • [x] I am on the latest Poetry version.

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

  • [x] If an exception occurs when executing a command, I executed it again in debug mode (-vvv option).

  • OS version and name: WSL, Ubuntu 20.04

  • Poetry version: 1.2.0b1

Issue

I'm relying on pyenv to specify which base Python version Poetry should be using. It was incredibly confusing, coming from an old version of Poetry, to find out that there's now a virtualenvs.prefer-active-python setting which defaults to false - so has to be set to true to use Poetry with pyenv. While that fixes e.g. poetry install, poetry env info still ignores pyenv even with this setting. To get it to provide information about my actual venv I had to wrap the call with poetry run (for it to pick up pyenv python correctly), i.e. poetry run poetry env info --path.

As a side note, this new prefer-active-python setting defauling to false is such an incredible gotcha. I would think the vast majority of (linux) users would use Poetry alongside pyenv (and everyone was using get-poetry.py installer in the past as it was the recommended one), and this will get them all.

nirvana-msu avatar Jul 03 '22 12:07 nirvana-msu

Hello @nirvana-msu,

can you please give an example about the output you see and the one you would expect?

fin swimmer

finswimmer avatar Jul 16 '22 16:07 finswimmer

Try installing poetry from the get-poetry.py installer.

  1. Clear poetry installation with
curl -sSL https://install.python-poetry.org | python - --uninstall
rm -rf .cache/pypoetry
  1. Re-install it with the "deprecated" script
curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python -

and let us know if something changes

matbun avatar Jul 27 '22 08:07 matbun

@finswimmer I have encountered the same issue, so I can give an example of unexpected behavior.

Setup

My system python is 3.9.11, as is the python poetry is installed against:

$ poetry debug info

Poetry
Version: 1.2.0
Python:  3.9.11

I take the following pyproject.toml, trying to use python 3.7 for my project:

[tool.poetry]
name = "example_project"
version = "0.0.1"
description = "Demonstrating an issue."
license = "GNU GPL3"
authors = ["Jacopo Tissino <[email protected]>"]

[tool.poetry.dependencies]
python = ">=3.7,<3.8"

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

Then (to start, I'll also show the outcome of setting it to true later), I set

$ poetry config virtualenvs.prefer-active-python false

Then, I run

$ pyenv local 3.7.12
$ python --version
Python 3.7.12

Creating the (wrong) virtualenv

Now, (without knowing about the prefer-active-python setting) I'd expect poetry to be using the current python, so that I could run

$ poetry install
The currently activated Python version 3.9.11 is not supported by the project (>=3.7,<3.8).
Trying to find and use a compatible version. 
Using python3 (3.7.12)
Creating virtualenv example-project-fEs4NoC1-py3.7 in /home/jacopo/.cache/pypoetry/virtualenvs
Updating dependencies
Resolving dependencies... (0.1s)

Writing lock file

while, instead, I see this weird warning: the currently activated python is not 3.9.11!

Incidentally, in the virtualenv's folder I find the unexpected structure: .../example-project-fEs4NoC1-py3.7/lib/python3.9/site-packages/...

Now, if I try to enter a poetry shell I can spot the problem:

$ poetry shell
The currently activated Python version 3.9.11 is not supported by the project (>=3.7,<3.8).
Trying to find and use a compatible version. 
Using python3 (3.7.12)
Spawning shell within /home/jacopo/.cache/pypoetry/virtualenvs/example-project-fEs4NoC1-py3.7
. /home/jacopo/.cache/pypoetry/virtualenvs/example-project-fEs4NoC1-py3.7/bin/activate
$ . /home/jacopo/.cache/pypoetry/virtualenvs/example-project-fEs4NoC1-py3.7/bin/activate
(example-project-py3.7) 18:22:48 jacopo powermetal /home/jacopo/Documents/examples/mwe_poetry 
$ python
Python 3.9.11 (main, Jun 10 2022, 15:11:46) 
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

env info issues

To relate to the poetry env info in the question: if I run it following the previous commands (so, with the experimental option off) I get

$ poetry env info

Virtualenv
Python:         3.9.11
Implementation: CPython
Path:           NA
Executable:     NA

System
Platform:   linux
OS:         posix
Python:     3.9.11
Path:       /home/jacopo/.pyenv/versions/3.9.11
Executable: /home/jacopo/.pyenv/versions/3.9.11/bin/python3.9

Alternatively, I can run

$ poetry run poetry env info
The currently activated Python version 3.9.11 is not supported by the project (>=3.7,<3.8).
Trying to find and use a compatible version. 
Using python3 (3.7.12)

Virtualenv
Python:         3.9.11
Implementation: CPython
Path:           /home/jacopo/.cache/pypoetry/virtualenvs/example-project-fEs4NoC1-py3.7
Executable:     /home/jacopo/.cache/pypoetry/virtualenvs/example-project-fEs4NoC1-py3.7/bin/python
Valid:          True

System
Platform:   linux
OS:         posix
Python:     3.9.11
Path:       /home/jacopo/.pyenv/versions/3.9.11
Executable: /home/jacopo/.pyenv/versions/3.9.11/bin/python3.9

which is slightly more informative (why do we need to wrap a poetry command with poetry run, though?).

Experimental option on

If I repeat all the previous procedure with $ poetry config virtualenvs.prefer-active-python true I get the correct virtual environment:

$ poetry shell
Spawning shell within /home/jacopo/.cache/pypoetry/virtualenvs/example-project-fEs4NoC1-py3.7
. /home/jacopo/.cache/pypoetry/virtualenvs/example-project-fEs4NoC1-py3.7/bin/activate
$ . /home/jacopo/.cache/pypoetry/virtualenvs/example-project-fEs4NoC1-py3.7/bin/activate
(example-project-py3.7) 18:28:39 jacopo powermetal /home/jacopo/Documents/examples/mwe_poetry 
$ python
Python 3.7.12 (default, Mar 22 2022, 16:21:33) 
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

but indeed, the env info command gives the wrong results: like before,

$ poetry env info

Virtualenv
Python:         3.9.11
Implementation: CPython
Path:           NA
Executable:     NA

System
Platform:   linux
OS:         posix
Python:     3.9.11
Path:       /home/jacopo/.pyenv/versions/3.9.11
Executable: /home/jacopo/.pyenv/versions/3.9.11/bin/python3.9

while, as @nirvana-msu noted,

$ poetry run poetry env info

Virtualenv
Python:         3.7.12
Implementation: CPython
Path:           /home/jacopo/.cache/pypoetry/virtualenvs/example-project-fEs4NoC1-py3.7
Executable:     /home/jacopo/.cache/pypoetry/virtualenvs/example-project-fEs4NoC1-py3.7/bin/python
Valid:          True

System
Platform:   linux
OS:         posix
Python:     3.7.12
Path:       /home/jacopo/.pyenv/versions/3.7.12
Executable: /home/jacopo/.pyenv/versions/3.7.12/bin/python3.7

This behavior does feel inconsistent to me, but let me know if I've missed anything! This is all using poetry 1.2.0 + pyenv 2.2.5, on ubuntu 22.04.

jacopok avatar Sep 03 '22 16:09 jacopok

Thanks a lot for these information @jacopok :+1: I try to have a look on this in the next week.

finswimmer avatar Sep 03 '22 17:09 finswimmer

I second this, it is not working as expected

ccostello97 avatar Sep 21 '22 19:09 ccostello97

I had a similar problem and it seems to come from the file ~/.cache/pypoetry/virtualenvs/envs.toml/ Removing ~/.cache/pypoetry/ solved my issue.

Saelyos avatar Sep 26 '22 09:09 Saelyos

Similarly, I ran into this issue (on mac) & resolved it by removing ~/Library/Application Support/pypoetry and re-installing.

kaszklarvts avatar Nov 07 '22 22:11 kaszklarvts

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

github-actions[bot] avatar Mar 01 '24 00:03 github-actions[bot]