With pyproject.toml override a dependency with an extra

I have a package with a dependency that should be there by default. I also have an optional dependency which should override the first one. Is this possible?

The use case is onnxruntime as a dependency and onnxruntime-gpu as an extra dependency when building for gpu. The issue is that onnxruntime-gpu cannot co-exist with onnxruntime (well it can, but GPU won’t actually work - onnxruntime-gpu has to be installed on its own)

So if I have something like this:

dependencies = [ "onnxruntime"]

[project.optional-dependencies]
gpu = [ "onnxruntime-gpu"]

this wouldn’t work as it would install both.

I could do something like this

dependencies = []

[project.optional-dependencies]
gpu = [ "onnxruntime-gpu"]
cpu = [ "onnxruntime"]

But then pip install mypackage would result in an incomplete environment. If that’s the best option, then that’s fine but thought I’d ask.

Have you tried something like the following?

dependencies = ['onnxruntime; extra != "gpu"']

# ...

Related:

1 Like

I had not - thanks for the tip. I did try it and it doesn’t seem to help -

dependencies = [
    'onnxruntime==1.17.1; extra != "gpu"',
]

But when I build that and install I get

Successfully installed onnxruntime-1.17.1 onnxruntime-gpu-1.17.1

I only saw this mentioned here but as a suggestion not as something that works: Optional dependency groups omitting package requirements - #32 by ntessore

A workaround is to have 2 versions of your package with a different set of dependencies, as did onnxruntime. It’s not ideal, but it’s a propagation of the design choices made by onnxruntime.