Skip to content

Commit 70d13e8

Browse files
authored
Merge branch 'main' into refactor/namespace-pkg-star
2 parents 5a28b4b + b40d96a commit 70d13e8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+770
-157
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ END_UNRELEASED_TEMPLATE
9191
also retrieved from the URL as opposed to only the `--hash` parameter. Fixes
9292
[#2363](https://github.com/bazel-contrib/rules_python/issues/2363).
9393
* (pypi) `whl_library` now infers file names from its `urls` attribute correctly.
94+
* (py_test, py_binary) Allow external files to be used for main
9495

9596
{#v0-0-0-added}
9697
### Added

docs/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ sphinx_stardocs(
113113
"//python/private:builders_util_bzl",
114114
"//python/private:py_binary_rule_bzl",
115115
"//python/private:py_cc_toolchain_rule_bzl",
116+
"//python/private:py_info_bzl",
116117
"//python/private:py_library_rule_bzl",
117118
"//python/private:py_runtime_rule_bzl",
118119
"//python/private:py_test_rule_bzl",

docs/_includes/py_console_script_binary.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,26 @@ py_console_script_binary(
4848
)
4949
```
5050

51+
#### Adding a Shebang Line
52+
53+
You can specify a shebang line for the generated binary, useful for Unix-like
54+
systems where the shebang line determines which interpreter is used to execute
55+
the script, per [PEP441]:
56+
57+
```starlark
58+
load("@rules_python//python/entry_points:py_console_script_binary.bzl", "py_console_script_binary")
59+
60+
py_console_script_binary(
61+
name = "black",
62+
pkg = "@pip//black",
63+
shebang = "#!/usr/bin/env python3",
64+
)
65+
```
66+
67+
Note that to execute via the shebang line, you need to ensure the specified
68+
Python interpreter is available in the environment.
69+
70+
5171
#### Using a specific Python Version directly from a Toolchain
5272
:::{deprecated} 1.1.0
5373
The toolchain specific `py_binary` and `py_test` symbols are aliases to the regular rules.
@@ -70,4 +90,5 @@ py_console_script_binary(
7090
```
7191

7292
[specification]: https://packaging.python.org/en/latest/specifications/entry-points/
73-
[`py_console_script_binary.binary_rule`]: #py_console_script_binary_binary_rule
93+
[`py_console_script_binary.binary_rule`]: #py_console_script_binary_binary_rule
94+
[PEP441]: https://peps.python.org/pep-0441/#minimal-tooling-the-zipapp-module

docs/api/rules_python/python/bin/index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
A target to directly run a Python interpreter.
1111

1212
By default, it uses the Python version that toolchain resolution matches
13-
(typically the one marked `is_default=True` in `MODULE.bazel`).
13+
(typically the one set with `python.defaults(python_version = ...)` in
14+
`MODULE.bazel`).
1415

1516
This runs a Python interpreter in a similar manner as when running `python3`
1617
on the command line. It can be invoked using `bazel run`. Remember that in

docs/toolchains.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ you should read the dev-only library module section.
4444
bazel_dep(name="rules_python", version=...)
4545
python = use_extension("@rules_python//python/extensions:python.bzl", "python")
4646
47-
python.toolchain(python_version = "3.12", is_default = True)
47+
python.defaults(python_version = "3.12")
48+
python.toolchain(python_version = "3.12")
4849
```
4950

5051
### Library modules
@@ -72,7 +73,8 @@ python = use_extension(
7273
dev_dependency = True
7374
)
7475
75-
python.toolchain(python_version = "3.12", is_default=True)
76+
python.defaults(python_version = "3.12")
77+
python.toolchain(python_version = "3.12")
7678
```
7779

7880
#### Library modules without version constraints
@@ -161,9 +163,13 @@ Multiple versions can be specified and used within a single build.
161163
# MODULE.bazel
162164
python = use_extension("@rules_python//python/extensions:python.bzl", "python")
163165

166+
python.defaults(
167+
# The environment variable takes precedence if set.
168+
python_version = "3.11",
169+
python_version_env = "BAZEL_PYTHON_VERSION",
170+
)
164171
python.toolchain(
165172
python_version = "3.11",
166-
is_default = True,
167173
)
168174

169175
python.toolchain(
@@ -264,7 +270,8 @@ bazel_dep(name = "rules_python", version = "0.40.0")
264270

265271
python = use_extension("@rules_python//python/extensions:python.bzl", "python")
266272

267-
python.toolchain(is_default = True, python_version = "3.10")
273+
python.defaults(python_version = "3.10")
274+
python.toolchain(python_version = "3.10")
268275

269276
use_repo(python, "python_3_10", "python_3_10_host")
270277
```

examples/bzlmod/MODULE.bazel

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,13 @@ bazel_dep(name = "rules_rust", version = "0.54.1")
2828
# We next initialize the python toolchain using the extension.
2929
# You can set different Python versions in this block.
3030
python = use_extension("@rules_python//python/extensions:python.bzl", "python")
31+
python.defaults(
32+
# Use python.defaults if you have defined multiple toolchain versions.
33+
python_version = "3.9",
34+
python_version_env = "BAZEL_PYTHON_VERSION",
35+
)
3136
python.toolchain(
3237
configure_coverage_tool = True,
33-
# Only set when you have multiple toolchain versions.
34-
is_default = True,
3538
python_version = "3.9",
3639
)
3740

examples/bzlmod/other_module/MODULE.bazel

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,16 @@ PYTHON_NAME_39 = "python_3_9"
2525
PYTHON_NAME_311 = "python_3_11"
2626

2727
python = use_extension("@rules_python//python/extensions:python.bzl", "python")
28+
python.defaults(
29+
# In a submodule this is ignored
30+
python_version = "3.11",
31+
)
2832
python.toolchain(
2933
configure_coverage_tool = True,
3034
python_version = "3.9",
3135
)
3236
python.toolchain(
3337
configure_coverage_tool = True,
34-
# In a submodule this is ignored
35-
is_default = True,
3638
python_version = "3.11",
3739
)
3840

examples/bzlmod/requirements_lock_3_9.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,9 @@ s3cmd==2.1.0 \
262262
--hash=sha256:49cd23d516b17974b22b611a95ce4d93fe326feaa07320bd1d234fed68cbccfa \
263263
--hash=sha256:966b0a494a916fc3b4324de38f089c86c70ee90e8e1cae6d59102103a4c0cc03
264264
# via -r examples/bzlmod/requirements.in
265-
setuptools==65.6.3 \
266-
--hash=sha256:57f6f22bde4e042978bcd50176fdb381d7c21a9efa4041202288d3737a0c6a54 \
267-
--hash=sha256:a7620757bf984b58deaf32fc8a4577a9bbc0850cf92c20e1ce41c38c19e5fb75
265+
setuptools==78.1.1 \
266+
--hash=sha256:c3a9c4211ff4c309edb8b8c4f1cbfa7ae324c4ba9f91ff254e3d305b9fd54561 \
267+
--hash=sha256:fcc17fd9cd898242f6b4adfaca46137a9edef687f43e6f78469692a5e70d851d
268268
# via
269269
# babel
270270
# yamllint

examples/bzlmod_build_file_generation/MODULE.bazel

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,13 @@ python = use_extension("@rules_python//python/extensions:python.bzl", "python")
4646

4747
# We next initialize the python toolchain using the extension.
4848
# You can set different Python versions in this block.
49+
python.defaults(
50+
# The environment variable takes precedence if set.
51+
python_version = "3.9",
52+
python_version_env = "BAZEL_PYTHON_VERSION",
53+
)
4954
python.toolchain(
5055
configure_coverage_tool = True,
51-
is_default = True,
5256
python_version = "3.9",
5357
)
5458

examples/multi_python_versions/MODULE.bazel

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ python.defaults(
1717
)
1818
python.toolchain(
1919
configure_coverage_tool = True,
20-
# Only set when you have mulitple toolchain versions.
21-
is_default = True,
2220
python_version = "3.9",
2321
)
2422
python.toolchain(

0 commit comments

Comments
 (0)