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:
[project]
name = 'my-package'
dependencies = [
'pandas',
'numpy>=1.22.0',
# ...
]
[project.optional-dependencies]
# development dependency groups
test = [
'my-package[chem]',
'pytest>=4.6',
'pytest-cov',
# ...
# Redundant overlap with chem and torch dependencies
'rdkit',
# ...
'torch>=1.9',
# ...
]
# feature dependency groups
chem = [
'rdkit',
# ...
# Redundant overlap with torch dependencies
'torch>=1.9',
# ...
]
torch = [
'torch>=1.9',
# ...
]
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:
[project]
name = 'my-package'
dependencies = [
'pandas',
'numpy>=1.22.0',
# ...
]
[project.optional-dependencies]
# development dependency groups
test = [
'my-package[chem]',
'pytest>=4.6',
'pytest-cov',
# ...
]
# feature dependency groups
chem = [
'my-package[torch]',
'rdkit',
# ...
]
torch = [
'torch>=1.9',
# ...
]
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?
my-package
is private have to do with your issue? Can you clarify this part? -- From what I can tell this should work, you already found the solution. -- As far as I know Poetry would not help, it has something called "dependency groups" that are not the same thing as "extras", but they are for development dependencies only, so not what you want. – Pitchstonemy-package[torch]
as achem
group dependency, it will install from the repository's latest hosted version instead of installing the newly specifiedtorch
group in the pyproject.toml. I tried using the dot syntax, e.g..[torch]
, hoping that would work but apparently pip doesn't recognize it within pyproject.toml files. And good to know about the Poetry differences! I'm not too familiar with Poetry yet – Studbookpython -m pip install 'my-package[chem] @ git+https://github.com/user/my-package.git'
then the dependencymy-package[torch]
ends up triggering pip to install from the index instead of from git? Is that right? If yes, that might be a bug in pip, if not, then I still have not understood what you mean. – Pitchstone'my-package[chem]'
) works so long asmy-package
is not hosted in an index somewhere. If I change the name to something that doesn't exist on pypi or on myPIP_EXTRA_INDEX_URL
, then it works fine. Otherwise, like I mentioned earlier, pip seems to prioritize grabbing the already-hosted package instead of from the file's groups. @Pitchstone you're correct in your understanding except it's installing from pypi/pip indices not git. Sorry for the confusion, should I start a new question or edit this? – Studbook