An example of how to configure bazel (via the new bzlmod MODULE.bazel
) for Python
projects that use a src
dir and have tests outside of the package in a separate
tests
dir.
- All python code is in a
src
dir, as recommended by the Python Packaging User Guide. - Tests are outside of the package and thus not shipped with distributions.
- The package must be installed before tests will work.
- Typically this is done via editable installs
pip install -e .
.
- Typically this is done via editable installs
- Clone the repo.
- Make a venv and activate it.
- Install the package
pip install -e .[dev]
- Make sure non-bazel basic unit tests work
python -m unittest
. - And make sure bazel unit tests work
bazel test //...
. - And that bazel run works:
bazel run //src/mypackage:mypackage_bin
.
If I've set up this example correctlly, everything should pass.
Buildifier is a linter and autoformatter for Starlark, the language used by Bazel.
Run with one or both of:
bazel run //:buildifier.fix
bazel run //:buildifier.check
Gazelle is a tool for autogenerating BUILD(.bazel)
files from source
code.
Run by calling all these, in order:
# If any python dependencies change:
bazel run //:requirements.update
bazel run //:gazelle_python_manifest.update
# Run gazelle and generate BUILD files and targets:
bazel run //:gazelle