It would be great if there was a way to remove/omit default requirements using optional dependency groups.
Currently the docs on optional dependencies doesn’t mention this, so I assume it’s not possible at the moment?
pseudo code pyproject.toml
example of what I’m suggestion
[project]
name = "Package-A"
dependencies = [
"foobar>=1",
]
[project.optional-dependencies]
compiled = {"omit" = ["foobar"], "add": ["foobar-compiled>=1"]}
There are a number of use cases for this:
Switching a dependency
Switching from a default dependency to a substitute package with slightly different characteristics, e.g. compiled/binary, GPU implementation, smaller etc.
The main package at runtime can then do try: import foobar; except ImportError; import foobar_compiled as foobar
.
My own use case is to make pydantic smaller, I’d like to compile and upload a pydantic-core-lite
package which omits certain dependencies at compile time in order to minimise binary size.
The problem is that since pydantic has (/will have) pydantic-core
(with slightly larger binaries) as a default dependency, adding a pydantic[lite]
optional dependency doesn’t help - pydantic-core
will still be installed.
I’d like to be able to have an optional dependency group with:
[project.optional-dependencies]
lite = {"omit" = ["pydantic-core"], "add": ["pydantic-core-lite"]}
Production Mode
Creating a lite
or production
group which removes dependencies which are installed by default to aid development.
For example uvicorn has a standard
group which installs some “cython-based dependencies” and other “optional extras” to aid in development.
It would be much easier to get started with if pip install uvicorn
installed everything you needed for development, then pip install uvicorn[prod]
installed the things you most likely need for production.
(I haven’t spoken to the maintainers of uvicorn about this, I’ll reach out to them)
[none] group
You could also imagine a pip install the-package[none]
mode where no dependencies are installed and users can decide exactly what they want to install and how manually.
To be clear: packages listed under omit
wouldn’t be uninstalled, and having them installed (e.g. manually, or as a dependency of another package) wouldn’t be an error, it would just tell pip not to bother installing that package as a dependency of the package in question.
I think adding this feature would be a real win for python packing, pip and the whole community.
I also imagine it could be achieved with minimal compatibility issues, since the only change visible to users and installers is a change in the schema for the optional-dependencies
bit of pyprojec.toml/setup.cfg/setup.py
.