Skip to content

Commit 1f17637

Browse files
authored
docs: document how to configure MODULE.bazel for different use cases (#1864)
This largely comes from being regularly tagged on BCR PRs about how a module should be configured. This info was sort of already in the getting started docs, but that isn't an obvious place to go and bloated the what should be simplified steps to getting started.
1 parent 9a638ea commit 1f17637

File tree

3 files changed

+246
-107
lines changed

3 files changed

+246
-107
lines changed

docs/sphinx/getting-started.md

Lines changed: 22 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
# Getting started
22

3-
The following two sections cover using `rules_python` with bzlmod and
4-
the older way of configuring bazel with a `WORKSPACE` file.
3+
This doc is a simplified guide to help get started started quickly. It provides
4+
a simplified introduction to having a working Python program for both bzlmod
5+
and the older way of using `WORKSPACE`.
56

7+
It assumes you have a `requirements.txt` file with your PyPI dependencies.
68

7-
## Using bzlmod
9+
For more details information about configuring `rules_python`, see:
10+
* [Configuring the runtime](toolchains)
11+
* [Configuring third party dependencies (pip/pypi)](pypi-dependencies)
12+
* [API docs](api/index)
813

9-
**IMPORTANT: bzlmod support is still in Beta; APIs are subject to change.**
14+
## Using bzlmod
1015

1116
The first step to using rules_python with bzlmod is to add the dependency to
1217
your MODULE.bazel file:
@@ -15,99 +20,20 @@ your MODULE.bazel file:
1520
# Update the version "0.0.0" to the release found here:
1621
# https://github.com/bazelbuild/rules_python/releases.
1722
bazel_dep(name = "rules_python", version = "0.0.0")
18-
```
19-
20-
Once added, you can load the rules and use them:
21-
22-
```starlark
23-
load("@rules_python//python:py_binary.bzl", "py_binary")
24-
25-
py_binary(...)
26-
```
27-
28-
Depending on what you're doing, you likely want to do some additional
29-
configuration to control what Python version is used; read the following
30-
sections for how to do that.
31-
32-
### Toolchain registration with bzlmod
3323

34-
A default toolchain is automatically configured depending on
35-
`rules_python`. Note, however, the version used tracks the most recent Python
36-
release and will change often.
37-
38-
If you want to use a specific Python version for your programs, then how
39-
to do so depends on if you're configuring the root module or not. The root
40-
module is special because it can set the *default* Python version, which
41-
is used by the version-unaware rules (e.g. `//python:py_binary.bzl` et al). For
42-
submodules, it's recommended to use the version-aware rules to pin your programs
43-
to a specific Python version so they don't accidentally run with a different
44-
version configured by the root module.
45-
46-
#### Configuring and using the default Python version
47-
48-
To specify what the default Python version is, set `is_default = True` when
49-
calling `python.toolchain()`. This can only be done by the root module; it is
50-
silently ignored if a submodule does it. Similarly, using the version-unaware
51-
rules (which always use the default Python version) should only be done by the
52-
root module. If submodules use them, then they may run with a different Python
53-
version than they expect.
54-
55-
```starlark
56-
python = use_extension("@rules_python//python/extensions:python.bzl", "python")
57-
58-
python.toolchain(
24+
pip = use_extension("@rules_python//python/extensions:pip.bzl", "pip")
25+
pip.parse(
26+
hub_name = "my_deps",
5927
python_version = "3.11",
60-
is_default = True,
28+
requirements_lock = "//:requirements.txt",
6129
)
30+
use_repo(pip, "my_deps")
6231
```
6332

64-
Then use the base rules from e.g. `//python:py_binary.bzl`.
65-
66-
#### Pinning to a Python version
67-
68-
Pinning to a version allows targets to force that a specific Python version is
69-
used, even if the root module configures a different version as a default. This
70-
is most useful for two cases:
71-
72-
1. For submodules to ensure they run with the appropriate Python version
73-
2. To allow incremental, per-target, upgrading to newer Python versions,
74-
typically in a mono-repo situation.
75-
76-
To configure a submodule with the version-aware rules, request the particular
77-
version you need, then use the `@python_versions` repo to use the rules that
78-
force specific versions:
79-
80-
```starlark
81-
python = use_extension("@rules_python//python/extensions:python.bzl", "python")
82-
83-
python.toolchain(
84-
python_version = "3.11",
85-
)
86-
use_repo(python, "python_versions")
87-
```
88-
89-
Then use e.g. `load("@python_versions//3.11:defs.bzl", "py_binary")` to use
90-
the rules that force that particular version. Multiple versions can be specified
91-
and use within a single build.
92-
93-
For more documentation, see the bzlmod examples under the {gh-path}`examples`
94-
folder. Look for the examples that contain a `MODULE.bazel` file.
95-
96-
#### Other toolchain details
97-
98-
The `python.toolchain()` call makes its contents available under a repo named
99-
`python_X_Y`, where X and Y are the major and minor versions. For example,
100-
`python.toolchain(python_version="3.11")` creates the repo `@python_3_11`.
101-
Remember to call `use_repo()` to make repos visible to your module:
102-
`use_repo(python, "python_3_11")`
103-
10433
## Using a WORKSPACE file
10534

106-
To import rules_python in your project, you first need to add it to your
107-
`WORKSPACE` file, using the snippet provided in the
108-
[release you choose](https://github.com/bazelbuild/rules_python/releases)
109-
110-
To depend on a particular unreleased version, you can do the following:
35+
Using WORKSPACE is deprecated, but still supported, and a bit more involved than
36+
using Bzlmod. Here is a simplified setup to download the prebuilt runtimes.
11137

11238
```starlark
11339
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
@@ -130,13 +56,7 @@ http_archive(
13056
load("@rules_python//python:repositories.bzl", "py_repositories")
13157

13258
py_repositories()
133-
```
134-
135-
### Toolchain registration
136-
137-
To register a hermetic Python toolchain rather than rely on a system-installed interpreter for runtime execution, you can add to the `WORKSPACE` file:
13859

139-
```starlark
14060
load("@rules_python//python:repositories.bzl", "python_register_toolchains")
14161

14262
python_register_toolchains(
@@ -157,25 +77,20 @@ pip_parse(
15777
)
15878
```
15979

160-
After registration, your Python targets will use the toolchain's interpreter during execution, but a system-installed interpreter
161-
is still used to 'bootstrap' Python targets (see https://github.com/bazelbuild/rules_python/issues/691).
162-
You may also find some quirks while using this toolchain. Please refer to [python-build-standalone documentation's _Quirks_ section](https://gregoryszorc.com/docs/python-build-standalone/main/quirks.html).
163-
164-
## Toolchain usage in other rules
165-
166-
Python toolchains can be utilized in other bazel rules, such as `genrule()`, by adding the `toolchains=["@rules_python//python:current_py_toolchain"]` attribute. You can obtain the path to the Python interpreter using the `$(PYTHON2)` and `$(PYTHON3)` ["Make" Variables](https://bazel.build/reference/be/make-variables). See the
167-
{gh-path}`test_current_py_toolchain <tests/load_from_macro/BUILD.bazel>` target for an example.
168-
16980
## "Hello World"
17081

171-
Once you've imported the rule set into your `WORKSPACE` using any of these
172-
methods, you can then load the core rules in your `BUILD` files with the following:
82+
Once you've imported the rule set using either Bzlmod or WORKSPACE, you can then
83+
load the core rules in your `BUILD` files with the following:
17384

17485
```starlark
17586
load("@rules_python//python:defs.bzl", "py_binary")
17687

17788
py_binary(
17889
name = "main",
17990
srcs = ["main.py"],
91+
deps = [
92+
"@my_deps//foo",
93+
"@my_deps//bar",
94+
]
18095
)
18196
```

docs/sphinx/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ by buildifier.
5757
self
5858
getting-started
5959
pypi-dependencies
60+
configuring
6061
pip
6162
coverage
6263
gazelle

0 commit comments

Comments
 (0)