What's the difference between extras and groups in poetry?

Viewed 1213

So far I have used poetry extras to install optional dependencies. For instance, in pyproject.toml I have defined

[tool.poetry.dependencies]
...
jupyter = { version = "^1.0.0", optional = true }

[tool.poetry.extras]
notebooks = ["jupyter"...]

and then I could install optional dependencies with poetry install -E notebooks.

Now I can see that poetry is going to support groups. My intuition is that the example above could be replaced with:

[tool.poetry.group.notebooks.dependencies]
jupyter = "^1.0.0"...

and then installed with poetry install --with notebooks.

Now I wonder how groups relate to extras.

  1. Are groups just a syntactic sugar that is going to simplify definition of optional dependencies?
  2. If yes, will extras be depracated in favour of groups?
  3. If not, what's the difference between them and how both can coexist?
2 Answers

Dependency groups in Poetry gives you the ability to put dependencies together you might only need in certain stages during development - installed alone or in combination with other groups.

The only mandatory group is the "main" group and contains all dependencies listed under [tool.poetry.dependencies]. The dependencies defined here are needed during runtime of your library/application and not only during development.

"Extras" are are concept in python packaging to define optional dependencies, that can be defined to provide optional features during runtime.

Due to the lack of an alternative, these extras are often misused, when using setuptools as a build backend, to define dependencies needed during development.

From the official documentation here https://python-poetry.org/docs/1.2/managing-dependencies/#dependency-groups

Dependency groups, other than the implicit main group, must only contain dependencies you need in your development process. Installing them is only possible by using Poetry. To declare a set of dependencies, which add additional functionality to the project during runtime, use extras instead. Extras can be installed by the end user using pip.

So if you plan to install something using pip - then extras is the choice.

Installing groups is only possible using Poetry.

Related