17

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?
dzieciou
  • 4,049
  • 8
  • 41
  • 85

3 Answers3

7

From the official documentation here https://python-poetry.org/docs/master/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.

Aljosha Koecher
  • 443
  • 6
  • 17
GopherM
  • 352
  • 2
  • 8
  • This is not the first place I read "add additional functionality to the project during runtime". What exactly is meant here by runtime? Is it possible to install a dependence during the python program execution? AFAIK extras is intended to be used during "installation-time" of the package under development. – dawid Jan 31 '23 at 14:32
  • @dawid adds functionality "during runtime", not installation itself during runtime. I think it's a wording that slightly confusing, nothing "extra" behind this – GopherM Jan 31 '23 at 17:01
  • Imagine you are writing a python library that you intend to publish. Dependency groups are for maintainers of that library, your CI pipeline etc. Extras are for the users of your library, when they are needed to provide optional features that all users might not need. For example, if you were writing an ORM called "dorm" (Dawid's ORM), you might provide "extras" for different databases, so your users could `pip install dorm[postges]` and get "dorm" + "psycopg2", or `pip install dorm[mysql]` and get "dorm" + "mysql-client". – Chris Lawlor Feb 02 '23 at 13:46
4

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.

finswimmer
  • 10,896
  • 3
  • 34
  • 44
4

'group' is something like you split the whole dependencies into multi files, such as requirements.txt, requirements_test.txt, requirements_doc.txt。You choose which requirements file to install based on your need, this usually happens during development.

'extra' is something like feature toggles of your published package, the external user choose which extras to install based on the need of his own project。

'group' is designed for internal developer, it applies for both package and application development. However, 'extra' is designed for external pypi user, it applies only for package.

Felix Yuan
  • 173
  • 2
  • 5