question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

PyEnv + Poetry: Poetry does not use correct Python versions

See original GitHub issue
  • I am on the latest Poetry version.

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

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

  • OS version and name: macOS 12.01

  • Poetry version: 1.1.13

Issue

I find it extremely challenging, i.e. impossible, to get poetry to use the correct Python version. I am using PyEnv to manage Python.

which python
/Users/XXX/.pyenv/shims/python
pyenv versions
  system
* 3.8.12 (set by /Users/XXX/projectA/.python-version)
  3.9.7
python -V
Python 3.8.12

BUT within projectA:

poetry shell

python -V
Python 3.10.2

Even though:

[tool.poetry]
name = "ABC"
version = "0.1.0"
description = ""
authors = ["XXX"]

[tool.poetry.dependencies]
python = "3.8.12"

[tool.poetry.dev-dependencies]

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

I have zero clue what I am doing wrong. Reading a couple of online resources did not help my understanding much. However obvious this is for core users, it is extremely frustrating and counterintuitive for me.

How to set this up correctly?

See also:

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:3
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

40reactions
finswimmercommented, Mar 1, 2022

Creating a venv with the current active Python on poetry install had only ever worked, when Poetry was installed via the get-poetry.py script. This was a side effect of the fact, that Poetry itself runs then with currently active Python version.

When Poetry was installed in any other way (install-poetry.py, pip, pipx) it will use the Python that was used during the installation, because Poetry now runs in its own environment and have no idea about your current active Python.

One can point Poetry to a specific Python binary that should be used to create the venv, by running poetry env use /path/to/python once per project before the venv creation. In case of pyenv this can look like this:

pyenv shell 3.8.12
poetry env use python3.8
poetry install

You can not use python as an argument for poetry env use here, because its forwarded to the underlying virtualenv which understand it as the system python and not the currently activated python.

With the next alpha release there will be an option experimental option virtualenvs.prefer-active-python (See https://github.com/python-poetry/poetry/pull/4852). Setting this to true, one should be able to switch to the desired Python version by pyenv before the venv creation and Poetry will than pick it up - regardless of the installation method.

fin swimmer

11reactions
thoughtfuldatacommented, Mar 22, 2022

@finswimmer Your solution doesnt work with a windows computer. I am using pyenv-win and even when I successfully create a shell it still installs the previous python version. I am attempting to go from python 3.9.6 to 3.9.11

These are my steps:

  1. pyenv shell 3.9.11
  2. poetry env use python ( i do check that python points to the 3.9.11)
  3. poetry install

poetry installs 3.9.6 instead of 3.9.11

I am using poetry version 1.1.12( I just realized I have an issue with updating to 1.1.13), Windows 11, Pyenv-win 2.64.11

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using Poetry with PyEnv and having Python version issues
My Ubuntu default python is 3.8 I believe. I'm trying to use python 3.10.0 for my development. I've done the following: pyenv install...
Read more >
Managing environments | Documentation - Poetry
By default, Poetry will try to use the Python version used during Poetry's installation to create the virtual environment for the current project....
Read more >
Get started with pyenv & poetry. Saviours in the python chaos!
Organise your python chaos with pyenv and poetry. This post shows how to set it up and some basic examples to get started....
Read more >
Modern Python part 1: start a project with pyenv & poetry
We are going to see here how to use two of them: Pyenv, to manage and install different Python versions, and Poetry, to...
Read more >
Managing Python Projects and Dependencies in 2022
Overall, I recommend using pyenv + poetry . I will ... Projects usually need a specific version of Python installed to work correctly....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found