|
| 1 | +# Python Poetry Rules for Bazel |
| 2 | +These rules are designed to allow you to easily use the [Poetry Package Manager](https://python-poetry.org/) with [Bazel](https://bazel.build/). It does this whilst still allowing you to use Poetry as usual with `poetry add` and `poetry run`. |
| 3 | + |
| 4 | +## Getting started |
| 5 | +To illustrate how to use this package, there's an [example project](./example) included which shows all the relevant wiring. |
| 6 | + |
| 7 | +## Poetry Setup |
| 8 | +In order to smoothen out the interactions between Bazel and Poetry, we use the common Python location of `.venv` for the Virtual Environment. This makes it easier for both tools to find it, this is configured using [the `virtualenvs.in-project` configuration with Poetry](https://python-poetry.org/docs/configuration/#virtualenvsin-project-boolean): |
| 9 | + |
| 10 | +``` |
| 11 | +poetry config virtualenvs.in-project true --local |
| 12 | +``` |
| 13 | + |
| 14 | +Which results in the [`poetry.toml` file found in our example project](./examples/poetry.toml). You can then use the normal Poetry commands. |
| 15 | + |
| 16 | +## Bazel Setup |
| 17 | +To enable Bazel to manage the Poetry Virtual Environment, we use the [`managed_directories` property in our example WORKSPACE](./examples/WORKSPACE); this lets Bazel recreate the environment within our workspace, and symlinks it into the Bazel environment: |
| 18 | + |
| 19 | +```py |
| 20 | +workspace( |
| 21 | + name = "basic_project", |
| 22 | + managed_directories = { |
| 23 | + "@poetry_environment": [".venv"] |
| 24 | + } |
| 25 | +) |
| 26 | +``` |
| 27 | + |
| 28 | +Afterwards, we use the `http_archive` package to download the rules: |
| 29 | + |
| 30 | +```py |
| 31 | +http_archive( |
| 32 | + name = "rules_python_poetry", |
| 33 | + url = "https://github.com/DaMouse404/rules_python_poetry/releases/download/0.0.1/rules_python_poetry-0.0.1.tar.gz", |
| 34 | + sha256 = "3ac54f1e9b070d2ed727c58f30798f4cea1123242279e7d6e1a48e1f06ca16d6", |
| 35 | +) |
| 36 | +``` |
| 37 | + |
| 38 | +Then we run the `poetry_enviromment` rule from `environment.bzl`, which generates the Virtual Environment if necessary, alongside symlinking the various Poetry configuration files into the Bazel environment: |
| 39 | + |
| 40 | +```py |
| 41 | +load("@rules_python_poetry//:environment.bzl", "poetry_environment") |
| 42 | +poetry_environment( |
| 43 | + name="poetry_environment", |
| 44 | + project="//:pyproject.toml", |
| 45 | + lock="//:poetry.lock", |
| 46 | + config="//:poetry.toml" |
| 47 | +) |
| 48 | +``` |
| 49 | + |
| 50 | +Then we can register the Poetry Python intrepreter as a toolchain for `PY3` in Bazel: |
| 51 | + |
| 52 | +```py |
| 53 | +register_toolchains("@poetry_environment//:poetry_toolchain") |
| 54 | +``` |
| 55 | + |
| 56 | +And finally we use `poetry export` to export requirements to `requirements.txt` format: |
| 57 | + |
| 58 | +```py |
| 59 | +load("@poetry_environment//:runtime.bzl", "interpreter_path") |
| 60 | +load("@poetry_environment//:export.bzl", "poetry_export") |
| 61 | +poetry_export( |
| 62 | + name="poetry_requirements" |
| 63 | +) |
| 64 | +``` |
| 65 | + |
| 66 | +Which can then be used to interact with [the standard `rules_python`](https://github.com/bazelbuild/rules_python) (take note of the `python_interpreter` passed to `pip_import` here to use the virtual env one): |
| 67 | + |
| 68 | +```py |
| 69 | +http_archive( |
| 70 | + name = "rules_python", |
| 71 | + url = "https://github.com/bazelbuild/rules_python/releases/download/0.0.2/rules_python-0.0.2.tar.gz", |
| 72 | + strip_prefix = "rules_python-0.0.2", |
| 73 | + sha256 = "b5668cde8bb6e3515057ef465a35ad712214962f0b3a314e551204266c7be90c", |
| 74 | +) |
| 75 | +load("@rules_python//python:repositories.bzl", "py_repositories") |
| 76 | +py_repositories() |
| 77 | +load("@rules_python//python:pip.bzl", "pip_repositories", "pip_import") |
| 78 | +pip_repositories() |
| 79 | + |
| 80 | +pip_import( |
| 81 | + name = "basic_project_pip", |
| 82 | + requirements = "@poetry_requirements//:requirements.txt", |
| 83 | + python_interpreter = interpreter_path |
| 84 | +) |
| 85 | +load("@basic_project_pip//:requirements.bzl", "pip_install") |
| 86 | +pip_install() |
| 87 | +``` |
0 commit comments