Skip to content

Commit 466da1d

Browse files
docs: using python_version attribute for specifying python version (#2589)
Updates examples and docs to tell to use the base rules and the python_version attribute instead of the wrapper transition rules.
1 parent 309ee59 commit 466da1d

File tree

3 files changed

+73
-15
lines changed

3 files changed

+73
-15
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ Unreleased changes template.
6262

6363
{#v0-0-0-fixed}
6464
### Fixed
65+
* (docs) Using `python_version` attribute for specifying python versions introduced in `v1.1.0`
6566
* (gazelle) Providing multiple input requirements files to `gazelle_python_manifest` now works correctly.
6667
* (pypi) Handle trailing slashes in pip index URLs in environment variables,
6768
fixes [#2554](https://github.com/bazelbuild/rules_python/issues/2554).

docs/_includes/py_console_script_binary.md

+15-6
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ py_console_script_binary(
1212
)
1313
```
1414

15-
Or for more advanced setups you can also specify extra dependencies and the
15+
#### Specifying extra dependencies
16+
You can also specify extra dependencies and the
1617
exact script name you want to call. It is useful for tools like `flake8`, `pylint`,
1718
`pytest`, which have plugin discovery methods and discover dependencies from the
1819
PyPI packages available in the `PYTHONPATH`.
@@ -34,17 +35,26 @@ py_console_script_binary(
3435
)
3536
```
3637

37-
A specific Python version can be forced by using the generated version-aware
38-
wrappers, e.g. to force Python 3.9:
38+
#### Using a specific Python version
39+
40+
A specific Python version can be forced by passing the desired Python version, e.g. to force Python 3.9:
3941
```starlark
40-
load("@python_versions//3.9:defs.bzl", "py_console_script_binary")
42+
load("@rules_python//python/entry_points:py_console_script_binary.bzl", "py_console_script_binary")
4143

4244
py_console_script_binary(
4345
name = "yamllint",
4446
pkg = "@pip//yamllint",
47+
python_version = "3.9"
4548
)
4649
```
4750

51+
#### Using a specific Python Version directly from a Toolchain
52+
:::{deprecated} 1.1.0
53+
The toolchain specific `py_binary` and `py_test` symbols are aliases to the regular rules.
54+
i.e. Deprecated `load("@python_versions//3.11:defs.bzl", "py_binary")` and `load("@python_versions//3.11:defs.bzl", "py_test")`
55+
56+
You should instead specify the desired python version with `python_version`; see above example.
57+
:::
4858
Alternatively, the [`py_console_script_binary.binary_rule`] arg can be passed
4959
the version-bound `py_binary` symbol, or any other `py_binary`-compatible rule
5060
of your choosing:
@@ -60,5 +70,4 @@ py_console_script_binary(
6070
```
6171

6272
[specification]: https://packaging.python.org/en/latest/specifications/entry-points/
63-
[`py_console_script_binary.binary_rule`]: #py_console_script_binary_binary_rule
64-
73+
[`py_console_script_binary.binary_rule`]: #py_console_script_binary_binary_rule

docs/toolchains.md

+57-9
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,9 @@ python = use_extension("@rules_python//python/extensions:python.bzl", "python")
116116
python.toolchain(python_version = "3.12")
117117
118118
# BUILD.bazel
119-
load("@python_versions//3.12:defs.bzl", "py_binary")
119+
load("@rules_python//python:py_binary.bzl", "py_binary")
120120
121-
py_binary(...)
121+
py_binary(..., python_version="3.12")
122122
```
123123

124124
### Pinning to a Python version
@@ -132,21 +132,59 @@ is most useful for two cases:
132132
typically in a mono-repo situation.
133133

134134
To configure a submodule with the version-aware rules, request the particular
135-
version you need, then use the `@python_versions` repo to use the rules that
136-
force specific versions:
135+
version you need when defining the toolchain:
137136

138137
```starlark
138+
# MODULE.bazel
139139
python = use_extension("@rules_python//python/extensions:python.bzl", "python")
140140

141141
python.toolchain(
142142
python_version = "3.11",
143143
)
144-
use_repo(python, "python_versions")
144+
use_repo(python)
145+
```
146+
147+
Then use the `@rules_python` repo in your BUILD file to explicity pin the Python version when calling the rule:
148+
149+
```starlark
150+
# BUILD.bazel
151+
load("@rules_python//python:py_binary.bzl", "py_binary")
152+
153+
py_binary(..., python_version = "3.11")
154+
py_test(..., python_version = "3.11")
145155
```
146156

147-
Then use e.g. `load("@python_versions//3.11:defs.bzl", "py_binary")` to use
148-
the rules that force that particular version. Multiple versions can be specified
149-
and use within a single build.
157+
Multiple versions can be specified and used within a single build.
158+
159+
```starlark
160+
# MODULE.bazel
161+
python = use_extension("@rules_python//python/extensions:python.bzl", "python")
162+
163+
python.toolchain(
164+
python_version = "3.11",
165+
is_default = True,
166+
)
167+
168+
python.toolchain(
169+
python_version = "3.12",
170+
)
171+
172+
# BUILD.bazel
173+
load("@rules_python//python:py_binary.bzl", "py_binary")
174+
load("@rules_python//python:py_test.bzl", "py_test")
175+
176+
# Defaults to 3.11
177+
py_binary(...)
178+
py_test(...)
179+
180+
# Explicitly use Python 3.11
181+
py_binary(..., python_version = "3.11")
182+
py_test(..., python_version = "3.11")
183+
184+
# Explicitly use Python 3.12
185+
py_binary(..., python_version = "3.12")
186+
py_test(..., python_version = "3.12")
187+
```
150188

151189
For more documentation, see the bzlmod examples under the {gh-path}`examples`
152190
folder. Look for the examples that contain a `MODULE.bazel` file.
@@ -159,6 +197,16 @@ The `python.toolchain()` call makes its contents available under a repo named
159197
Remember to call `use_repo()` to make repos visible to your module:
160198
`use_repo(python, "python_3_11")`
161199

200+
201+
:::{deprecated} 1.1.0
202+
The toolchain specific `py_binary` and `py_test` symbols are aliases to the regular rules.
203+
i.e. Deprecated `load("@python_versions//3.11:defs.bzl", "py_binary")` & `load("@python_versions//3.11:defs.bzl", "py_test")`
204+
205+
Usages of them should be changed to load the regular rules directly;
206+
i.e. Use `load("@rules_python//python:py_binary.bzl", "py_binary")` & `load("@rules_python//python:py_test.bzl", "py_test")` and then specify the `python_version` when using the rules corresponding to the python version you defined in your toolchain. {ref}`Library modules with version constraints`
207+
:::
208+
209+
162210
#### Toolchain usage in other rules
163211

164212
Python toolchains can be utilized in other bazel rules, such as `genrule()`, by
@@ -508,4 +556,4 @@ of available toolchains.
508556
Currently the following flags are used to influence toolchain selection:
509557
* {obj}`--@rules_python//python/config_settings:py_linux_libc` for selecting the Linux libc variant.
510558
* {obj}`--@rules_python//python/config_settings:py_freethreaded` for selecting
511-
the freethreaded experimental Python builds available from `3.13.0` onwards.
559+
the freethreaded experimental Python builds available from `3.13.0` onwards.

0 commit comments

Comments
 (0)