Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you install an optional package when using Python Poetry?

Given the following pyproject.toml file, how do I install the optional package ipdb?

[tool.poetry]
name = "yolo"
version = "1.0.0"
description = ""
authors = []

[tool.poetry.dependencies]
python = "^3.8"
Django = "^4.0.4"
ipdb = {version = "^0.13.6", optional = true}

[tool.poetry.dev-dependencies]
pytest = "^5.4.1"

The Poetry Documentation shows how to add an optional package to the pyproject.toml file, but not how to actually install it if I am given the file and have a blank environment.

like image 838
Steven C. Howell Avatar asked Oct 19 '25 03:10

Steven C. Howell


1 Answers

Optional dependencies must be clusters within an extra to be installable: https://python-poetry.org/docs/pyproject/#extras

So, your pyproject.toml could look like this:

[tool.poetry]
name = "yolo"
version = "1.0.0"
description = ""
authors = []

[tool.poetry.dependencies]
python = "^3.8"
Django = "^4.0.4"
ipdb = {version = "^0.13.6", optional = true}

[tool.poetry.dev-dependencies]
pytest = "^5.4.1"

[tool.poetry.extras]
debug = ["ipdb"]

Than use poetry install -E debug to install this extra.

At the moment the CLI is missing an option to add an optional dependencies to an extra group.

Side note: extras are meant for additional functions of your package during runtime. If you only need ipdb during development, I would suggest to add it a as dev-dependencies.

like image 69
finswimmer Avatar answered Oct 21 '25 16:10

finswimmer