Does Poetry isolate optional extras per project in shared virtual environments?
0 reputation · 01 May 2024, 13:42 UTC
0 reputation · 01 May 2024, 13:42 UTC
Poetry's deterministic resolver builds a dependency graph across all declared packages and selects the highest compatible version. Optional dependencies are included in the lock file only when the corresponding extra is requested during installation, yet the resolution algorithm reportedly does not differentiate between mandatory and optional packages when computing that graph.
Because Poetry does not support per-project optional dependency isolation, optional packages can inadvertently be installed into shared virtual environments if they are not explicitly excluded. The lock file records these optional dependencies under a separate extras section, which raises uncertainty about whether the resolver treats them as first-class graph nodes during conflict resolution.
This behavior may differ between Poetry 1.x and the 2.x series, since the resolver algorithm was refactored in 1.2. The official documentation does not currently describe optional dependency handling in detail, leaving the expected isolation model unclear.
Does Poetry's resolver treat optional extras as isolated branches when computing the dependency graph, or are they fully connected nodes subject to the same conflict resolution as mandatory dependencies?
What is the expected isolation boundary for optional packages when multiple projects share a single virtual environment across Poetry 1.x and 2.x?
No. Poetry does not provide per-project isolation for optional extras when projects share a single virtual environment. Optional packages are not isolated branches in the resolver. When an extra is activated, its dependencies become regular nodes in the same dependency graph and are subject to the same conflict resolution as mandatory dependencies. In a shared venv, installing an extra for one project makes that package available to all projects using that environment.
extras section, and optional dependencies are only installed when the extra is explicitly requested, e.g. poetry install --extras <extra>.pyproject.toml. Isolation would require separate virtual environments or using --without/--only flags to exclude unwanted extras.The resolver was refactored across the 1.x series and continues into the 2.x series. The change improved determinism and performance, but it did not introduce per-project or per-extra isolation. Optional nodes are therefore fully connected to the rest of the graph rather than being isolated branches. This means version constraints from an activated extra can influence the version chosen for a shared dependency, and that version will be written to the shared environment.
poetry install --extras <extra-one> for the first project and inspect the environment with pip list and poetry show --tree.poetry install --extras <extra-two> for the second project and observe whether the first optional package is upgraded, downgraded, or remains.To give a precise recommendation for your setup, what Poetry version and how is the shared virtual environment created and used? Specifically, are you using poetry env use to point multiple projects to one venv, or a manually shared .venv path? That detail changes whether --without exclusion or separate environments is the safer mitigation.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.