rules_py
rules_py copied to clipboard
Demonstrate virtual dependencies
Virtual dependencies are metadata on a py_library that indicate the need for a given dependency, but do not resolve that dependency to a set of files. The virtual dependency is "resolved" at the terminal rule (e.g. py_binary
/py_test
), by use of the resolutions
parameter.
This way, any number of terminal rules can depend on a single py_library, and each of them can specify its own preferred resolutions. This permits three useful patterns:
- monorepos in which many Python projects coexist, but do not want to link their dependency versions together
- single Python projects that want to test against a matrix of different Python interpreters and library versions.
- mocking of external Python dependencies with simpler internal versions.
In the example given, I added a dependency on six
in the django-virtual project. If the dependency is removed from the virtuals
list in django-virtual/mysite/BUILD.bazel
, the generated virtualenv does not contain the six
library, and an ImportError is produced.
This implementation could potentially be extended to include Python requirements specifiers [1] for each virtual dependency (e.g. "six>=1.10"), to allow another tool (e.g. pip-compile) to gather all of the dependencies and resolve them into a requirements.txt file.
[1] https://pip.pypa.io/en/stable/reference/requirement-specifiers/