bazelbuild / bazelbuild/rules_docker
Support multiple python versions in py3_image's py_binary
- Dominant language
- Starlark
- Stars
- 1.1k
- Forks
- 689
- PR merge metrics
- No merged PRs in 30d
Description
# 🚀 feature request
### Relevant Rules
`py3_image`
### Description
We have multiple versions of python toolchains installed with [`python_multi_register_toolchains`](https://github.com/bazelbuild/rules_python/blob/64684ae0498576ad2a09fa528fed07afa5e7307d/python/repositories.bzl#L565)
Current implementation of `py3_image` always imports `py_binary` from `@rules_python//python:def.bzl`. This results in `py_binary` using the default version used in `python_multi_register_toolchains` and there aren't ways to specify other python version.
https://github.com/bazelbuild/rules_docker/blob/6db7c12fbe4b49682f5dcbc193e4c467011a9fb6/python3/image.bzl#L96-L102
### Describe the solution you'd like
Not sure what the solution could look like 🤔
### Describe alternatives you've considered
I'm using a temporarily patch to use other version's toolchain for now:
```diff
@@ -16,7 +16,9 @@
The signature of this rule is compatible with py_binary.
"""
-load("@rules_python//python:defs.bzl", "py_binary")
+load("@rules_python//python:defs.bzl", py_binary_default = "py_binary")
+load("@python//3.9:defs.bzl", py_binary_3_9 = "py_binary")
+load("@python//3.11:defs.bzl", py_binary_3_11 = "py_binary")
load(
"//container:container.bzl",
"container_pull",
@@ -73,7 +75,7 @@ DEFAULT_BASE = select({
"//conditions:default": "@py3_image_base//image",
})
-def py3_image(name, base = None, deps = [], layers = [], env = {}, **kwargs):
+def py3_image(name, base = None, deps = [], layers = [], env = {}, py_version = "", **kwargs):
"""Constructs a container image wrapping a py_binary target.
Args:
@@ -83,6 +85,7 @@ def py3_image(name, base = None, deps = [], layers = [], env = {}, **kwargs):
layers: Augments "deps" with dependencies that should be put into
their own layers.
env: Environment variables for the py_image.
+ py_version: Specific python binary version to use (e.g. 3.11)
**kwargs: See py_binary.
"""
binary_name = name + ".binary"
@@ -93,9 +96,14 @@ def py3_image(name, base = None, deps = [], layers = [], env = {}, **kwargs):
# TODO(mattmoor): Consider using par_binary instead, so that
# a single target can be used for all three.
+ py_binary = py_binary_default
+ if py_version == "3.9":
+ py_binary = py_binary_3_9
+ if py_version == "3.11":
+ py_binary = py_binary_3_11
+
py_binary(
name = binary_name,
- python_version = "PY3",
deps = deps + layers,
exec_compatible_with = ["@io_bazel_rules_docker//platforms:run_in_container"],
**kwargs
```
Contributor guide
Assessment
This issue has not been assessed yet.