pyproject.toml:可选依赖组能否要求其他可选依赖组?

huangapple go评论96阅读模式
英文:

Pip pyproject.toml: Can optional dependency groups require other optional dependency groups?

问题

I am using the latest version of pip, 23.01. I have a pyproject.toml file with dependencies and optional dependency groups (aka "extras"). To avoid redundancies and make managing optional dependency groups easier, I would like to know how to have optional dependency groups require other optional dependency groups.

I have a pyproject.toml where the optional dependency groups have redundant overlaps in dependencies. I guess they could be described as "hierarchical". It looks like this:

  1. [project]
  2. name = 'my-package'
  3. dependencies = [
  4. 'pandas',
  5. 'numpy>=1.22.0',
  6. # ...
  7. ]
  8. [project.optional-dependencies]
  9. # development dependency groups
  10. test = [
  11. 'my-package[chem]',
  12. 'pytest>=4.6',
  13. 'pytest-cov',
  14. # ...
  15. # Redundant overlap with chem and torch dependencies
  16. 'rdkit',
  17. # ...
  18. 'torch>=1.9',
  19. # ...
  20. ]
  21. # feature dependency groups
  22. chem = [
  23. 'rdkit',
  24. # ...
  25. # Redundant overlap with torch dependencies
  26. 'torch>=1.9',
  27. # ...
  28. ]
  29. torch = [
  30. 'torch>=1.9',
  31. # ...
  32. ]

In the above example, pip install .[test] will include all of chem and torch groups' packages, and pip install .[chem] will include torch group's packages.

Removing overlaps and references from one group to another, a user can still get packages required for chem by doing pip install .[chem,torch], but I work with data scientists who may not realize immediately that the torch group is a requirement for the chem group, etc.

Therefore, I want a file that's something like this:

  1. [project]
  2. name = 'my-package'
  3. dependencies = [
  4. 'pandas',
  5. 'numpy>=1.22.0',
  6. # ...
  7. ]
  8. [project.optional-dependencies]
  9. # development dependency groups
  10. test = [
  11. 'my-package[chem]',
  12. 'pytest>=4.6',
  13. 'pytest-cov',
  14. # ...
  15. ]
  16. # feature dependency groups
  17. chem = [
  18. 'my-package[torch]',
  19. 'rdkit',
  20. # ...
  21. ]
  22. torch = [
  23. 'torch>=1.9',
  24. # ...
  25. ]

This approach can't work because my-package is hosted in our private pip repository, so having 'my-package[chem]' like the above example fetches the previously built version's chem group packages.

It appears that using Poetry and its pyproject.toml format/features can make this possible, but I would prefer not to switch our build system around too much. Is this possible with pip?

英文:

I am using the latest version of pip, 23.01. I have a pyproject.toml file with dependencies and optional dependency groups (aka "extras"). To avoid redundancies and make managing optional dependency groups easier, I would like to know how to have optional dependency groups require other optional dependency groups.

I have a pyproject.toml where the optional dependency groups have redundant overlaps in dependencies. I guess they could described as "hierarchical". It looks like this:

  1. [project]
  2. name = 'my-package'
  3. dependencies = [
  4. 'pandas',
  5. 'numpy>=1.22.0',
  6. # ...
  7. ]
  8. [project.optional-dependencies]
  9. # development dependency groups
  10. test = [
  11. 'my-package[chem]',
  12. 'pytest>=4.6',
  13. 'pytest-cov',
  14. # ...
  15. # Redundant overlap with chem and torch dependencies
  16. 'rdkit',
  17. # ...
  18. 'torch>=1.9',
  19. # ...
  20. ]
  21. # feature dependency groups
  22. chem = [
  23. 'rdkit',
  24. # ...
  25. # Redundant overlap with torch dependencies
  26. 'torch>=1.9',
  27. # ...
  28. ]
  29. torch = [
  30. 'torch>=1.9',
  31. # ...
  32. ]

In the above example, pip install .[test] will include all of chem and torch groups' packages, and pip install .[chem] will include torch group's packages.

Removing overlaps and references from one group to another, a user can still get packages required for chem by doing pip install .[chem,torch], but I work with data scientists who may not realize immediately that the torch group is a requirement for the chem group, etc.

Therefore, I want a file that's something like this:

  1. [project]
  2. name = 'my-package'
  3. dependencies = [
  4. 'pandas',
  5. 'numpy>=1.22.0',
  6. # ...
  7. ]
  8. [project.optional-dependencies]
  9. # development dependency groups
  10. test = [
  11. 'my-package[chem]',
  12. 'pytest>=4.6',
  13. 'pytest-cov',
  14. # ...
  15. ]
  16. # feature dependency groups
  17. chem = [
  18. 'my-package[torch]',
  19. 'rdkit',
  20. # ...
  21. ]
  22. torch = [
  23. 'torch>=1.9',
  24. # ...
  25. ]

This approach can't work because my-package is hosted in our private pip repository, so having'my-package[chem]' like the above example fetches the previously built version's chem group packages.

It appears that using Poetry and its pyproject.toml format/features can make this possible, but I would prefer not to switch our build system around too much. Is this possible with pip?

答案1

得分: 4

也许值得将这2个依赖组合成一个,看看是否解决了问题,就像在 'all' 中所示:

  1. [project]
  2. name = "foo"
  3. version = "0.1.0"
  4. [project.optional-dependencies]
  5. socks = ["pysocks"]
  6. jwt = ["pyjwt"]
  7. all = ["foo[socks,jwt]"]
英文:

Maybe it's worth binding the 2 dependency groups into one, and see if that solves the problem, like shown in 'all'

  1. [project]
  2. name = "foo"
  3. version = "0.1.0"
  4. [project.optional-dependencies]
  5. socks = ["pysocks"]
  6. jwt = ["pyjwt"]
  7. all = ["foo[socks,jwt]"]
  8. </details>

huangapple
  • 本文由 发表于 2023年2月18日 07:52:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75490231.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定