How to change default Python version for poetry?

96 Views Asked by At

When using Poetry to create a new project with poetry new project-name, it consistently defaults to Python 3.10, despite having Python 3.12 installed on my Ubuntu system. Although I managed to configure Python's global version to 3.12, this adjustment occurred after installing Poetry. Consequently, Poetry persists in using Python 3.10 for new projects. Here's the contents of the pyproject.toml file that poetry created:

[tool.poetry]
name = "statements"
version = "0.1.0"
description = ""
authors = ["Sajid Munawar <[email protected]>"]
readme = "README.md"

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


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

I've attempted using poetry env use path/to/python3.12, which successfully updates the virtual environment for the current project. However, this command is specific to the current project and doesn't affect future projects created with Poetry.

How can I ensure that Poetry uses Python 3.12 by default for new projects, even after the global Python version has been updated?

1

There are 1 best solutions below

0
Kinuax On

I would suggest to install Poetry with the specific Python version that you need via pipx.

Steps:

  1. Uninstall poetry.

  2. Install pipx.

  3. Install poetry with the needed Python version.

    pipx install --python python3.12 poetry

  4. Create new environments with poetry so that pyproject.toml has python = "^3.12".

    poetry new statements