Yes, you can install all optional dependencies of a package using pip. When you define optional dependencies (also known as "extras") in your pyproject.toml file (using the project.optional-dependencies table, I assume), pip provides a way to install them all at once.
Here's the magic syntax:
bash
pip install '.[all]'
Note the dot (.) and the square brackets []. This tells pip to:
. : Install the package in the current directory (i.e., your project). This is instead of specifying the package name, like pip install my-package.
[all] : Install all optional dependencies (extras) defined in pyproject.toml.
When you run this command, pip will parse your pyproject.toml, gather all the dependencies listed under project.optional-dependencies, and install them alongside your package.
Here's a quick example to illustrate this:
Suppose your pyproject.toml contains:
toml
[project]
name = "my-awesome-package"
version = "1.0.0"
[project.optional-dependencies]
dev = ["black", "pytest"]
docs = ["sphinx", "sphinx-autodoc"]
gpu = ["cupy", "numba"]
[build-system]
requires = ["poetry>=0.12"]
build-backend = "poetry.masonry.api"
In this case, running:
bash
pip install '.[all]'
is equivalent to:
bash
pip install '.[dev,docs,gpu]'
or even:
bash
pip install 'my-awesome-package[dev,docs,gpu]'
All of these commands will install your package (my-awesome-package) along with all optional dependencies: black, pytest, sphinx, sphinx-autodoc, cupy, and numba.
Tips & Variations:
If you only want to install a subset of optional dependencies, specify them explicitly: pip install '.[dev,docs]'
To install a specific extra, use: pip install '.[gpu]'
If you're installing from a remote repository (e.g., GitHub), use the package name instead of . : pip install 'git+https://github.com/your/repo.git#egg=my-awesome-package[all]'
For editable installs ( -e flag), it works too: pip install -e '.[all]'
That's it! Now you can install all optional dependencies with a single command. Happy packaging! 🎉