python-poetry / poetry-plugin-shell

MIT License
0 stars 1 forks source link

Contraditory warning from `poetry shell` on Poetry 1.2.2 #11

Open staticdev opened 1 year ago

staticdev commented 1 year ago

Relevant part:

[tool.poetry.dependencies]
python = ">=3.10.0,<4.0"
click = ">=8.1.1"

Issue

I have many versions of Python installed with Pyenv, but my settings are:

pyenv local 3.10.6
pyenv global 3.10.6

I have created a virtualenv with poetry install and when I do poetry shell I get:

The currently activated Python version 3.9.2 is not supported by the project (>=3.10.0,<4.0).
Trying to find and use a compatible version. 
Using python3 (3.10.6)
Spawning shell within /home/user/.cache/pypoetry/virtualenvs/project-0KWPzb1N-py3.10

I don't understand this warning since my venv was created with py3.10, my pyenv only has 3.10 and it says python3 (3.10.6).

Then after poetry shell I get error with:

poetry run project
Current Python version (3.9.2) is not allowed by the project (>=3.10.0,<4.0).
Please change python executable via the "env use" command.

Successful env use 3.10.6 and again same error:

env use 3.10.6
Using virtualenv: /home/user/.cache/pypoetry/virtualenvs/project-0KWPzb1N-py3.10
poetry run project
Current Python version (3.9.2) is not allowed by the project (>=3.10.0,<4.0).
Please change python executable via the "env use" command.

I also tried deleting the folder /home/user/.cache/pypoetry/virtualenvs/project-0KWPzb1N-py3.10 and creating again the environment but I get the same warning and errors:

poetry install
The currently activated Python version 3.9.2 is not supported by the project (>=3.10.0,<4.0).
Trying to find and use a compatible version. 
Using python3 (3.10.6)
finswimmer commented 1 year ago

Hey @staticdev,

by default Poetry will use the interpreter it was installed with to create a new venv. If it should instead take whatever is the activated Python in your current shell set virtualenvs.prefer-active-python to true.

fin swimmer

staticdev commented 1 year ago

@finswimmer thanks a lot for the prompt response and clarification! It was very helpful =)

I did poetry config virtualenvs.prefer-active-python true and recreated the environment, everything worked. Still I think the behavior is not totally consistent with what you said. When I had poetry config virtualenvs.prefer-active-python false, I used poetry install, it was using python 3.9.2 (probably the version I installed it), but the folder created was named with py3.10. And also I find strange to say:

The currently activated Python version 3.9.2 is not supported by the project (>=3.10.0,<4.0).
Trying to find and use a compatible version. 
Using python3 (3.10.6)

This two points made me confused, even using poetry for years now. Questions:

  1. Would it be the case that, since poetry was using python 3.9.2 the environment folder has py3.9?
  2. Actually, since my project forbids python 3.9 with python = ">=3.10.0,<4.0" maybe then I should get an error right away. This would also prevent confusing messages of warning and error after this point.
staticdev commented 1 year ago

@finswimmer I am having same problem on Mac, but this time poetry config virtualenvs.prefer-active-python true is not my friend. I always have:

Current Python version ... is not allowed by the project...
Please change python execute via the "use env" command.

poetry use env also does not change the result.

bikefrivolously commented 1 year ago

I'm also experiencing a similar issue, which seems to contradict the documentation here: https://python-poetry.org/docs/managing-environments/

By default, Poetry will try to use the Python version used during Poetry’s installation to create the virtual environment for the current project. However, for various reasons, this Python version might not be compatible with the python requirement of the project. In this case, Poetry will try to find one that is and use it. If it’s unable to do so then you will be prompted to activate one explicitly, see Switching environments.

While I am using pyenv, I don't think the prefer-active-python setting is relevant here. I don't care if poetry uses the currently active version but I would like it to use a version that meets the project dependencies, and it looks like it is trying to :)

Poetry was installed using python 3.9.13 initially. I have 3.9.13 and 3.10.6 available via pyenv. My current project wants ^3.10

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

poetry should look for python 3.10.x and create the virtual environment using the discovered version, or prompt otherwise. From the output, it appears to find 3.10.6 but when I inspect the created virtual environment afterwards, it is python 3.9.13

$ poetry install
The currently activated Python version 3.9.13 is not supported by the project (^3.10).
Trying to find and use a compatible version. 
Using python3 (3.10.6)
Creating virtualenv flask-huey-DOCxGAqz-py3.10 in /home/dmartins/.cache/pypoetry/virtualenvs
....
$ poetry env info

Virtualenv
Python:         3.9.13
Implementation: CPython
Path:           /home/dmartins/.cache/pypoetry/virtualenvs/flask-huey-DOCxGAqz-py3.10
Executable:     /home/dmartins/.cache/pypoetry/virtualenvs/flask-huey-DOCxGAqz-py3.10/bin/python
Valid:          True

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

$ cat  /home/dmartins/.cache/pypoetry/virtualenvs/flask-huey-DOCxGAqz-py3.10/pyvenv.cfg 
home = /home/dmartins/.pyenv/versions/3.9.13/bin
implementation = CPython
version_info = 3.9.13.final.0
virtualenv = 20.16.7
include-system-site-packages = false
base-prefix = /home/dmartins/.pyenv/versions/3.9.13
base-exec-prefix = /home/dmartins/.pyenv/versions/3.9.13
base-executable = /home/dmartins/.pyenv/versions/3.9.13/bin/python3.9
prompt = flask-huey-py3.10

edit: including my current poetry config

cache-dir = "/home/dmartins/.cache/pypoetry"
experimental.new-installer = true
experimental.system-git-client = false
installer.max-workers = null
installer.no-binary = null
installer.parallel = true
virtualenvs.create = true
virtualenvs.in-project = null
virtualenvs.options.always-copy = false
virtualenvs.options.no-pip = false
virtualenvs.options.no-setuptools = false
virtualenvs.options.system-site-packages = false
virtualenvs.path = "{cache-dir}/virtualenvs"  # /home/dmartins/.cache/pypoetry/virtualenvs
virtualenvs.prefer-active-python = false
virtualenvs.prompt = "{project_name}-py{python_version}"

edit2: With virtualenvs.prefer-active-python = true:

With virtualenvs.prefer-active-python = false:

bikefrivolously commented 1 year ago

@staticdev a potential workaround for your initial issue is to run poetry env use before any venv with the -py3.10 suffix exists.

ShadowLNC commented 1 year ago

At a glance, this looks like it might be the same behaviour as python-poetry/poetry#7158