Home > Software design >  Poetry creates a project with wrong Python version
Poetry creates a project with wrong Python version

Time:12-23

I am attempting to create a poetry project with the latest version of python install via my pyenv installation with homebrew. The below terminal commands show the versions of pyenv and poetry that I have.

path/user % pyenv --version
pyenv 2.3.9
path/user % pyenv versions
  system
* 3.9.6 (set by /Users/user/.pyenv/version)
path/user % poetry --version
Poetry (version 1.3.1)
path/user % poetry new venv
Created package venv in venv
path/user % ls
venv
path/user % cd venv
(base) user@user venv % ls
README.md   pyproject.toml  tests       venv
(base) user@user venv % cat pyproject.toml 
[tool.poetry]
name = "venv"
version = "0.1.0"
description = ""
authors = ["user <[email protected]>"]
readme = "README.md"

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


[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
(base) user@user venv % poetry venv use 3.9.6

The command "venv" does not exist.
(base) user@user venv % poetry env 3.9.6

The command "env" does not exist.

Did you mean one of these?
    env use
    env info
    env list
    env remove
(base) user@user venv % poetry env use 3.9.6
The specified Python version (3.9.6) is not supported by the project (^3.11).
Please choose a compatible version or loosen the python constraint specified in the pyproject.toml file.

The issue I am having is that, despite only having python version 3.9.6 installed on my machine, the poetry project is being created with a default of python version 3.11 each time. What am I doing wrong and how do I solve the issue?

I attempted uninstalling and reinstalling both pyenv and poetry via homebrew. Before creating the poetry project, I also tried pyenv local 3.9.6, but this didn't work, either.

Goal: Be able to create a poetry project poetry new venv without having to manually update the pyproject.toml file each time.

CodePudding user response:

By default Poetry initializes new projects and creates new venvs with the Python version that was used for installing Poetry itself.

This is why you have python = "^3.11" in your pyproject.toml.

The specified Python version (3.9.6) is not supported by the project (^3.11).

This method tells you, that your project aimed to be compatible for python >=3.11,<4.0, but you are trying to use a python3.9 interpreter. You have to manual edit the pyproject.toml and set python = "^3.11" to whatever fits your needs.

To enable Poetry to pick up the currently activated version of Python, you have to set the config option virtualenvs.prefer-active-python to true. See https://python-poetry.org/docs/configuration/#virtualenvsprefer-active-python-experimental

Please note that at the moment this option is not respected on poetry new or poetry init. Until https://github.com/python-poetry/poetry/pull/7100 is merged and released you still have to edit the pyproject.toml by hand.

  • Related