Skip to content
This repository has been archived by the owner on Oct 2, 2023. It is now read-only.

Support multiple python versions in py3_image's py_binary #2243

Open
jumbosushi opened this issue Apr 3, 2023 · 1 comment
Open

Support multiple python versions in py3_image's py_binary #2243

jumbosushi opened this issue Apr 3, 2023 · 1 comment
Labels
Can Close? Will close in 30 days unless there is a comment indicating why not

Comments

@jumbosushi
Copy link

jumbosushi commented Apr 3, 2023

🚀 feature request

Relevant Rules

py3_image

Description

We have multiple versions of python toolchains installed with python_multi_register_toolchains

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.

py_binary(
name = binary_name,
python_version = "PY3",
deps = deps + layers,
exec_compatible_with = ["@io_bazel_rules_docker//platforms:run_in_container"],
**kwargs
)

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:

@@ -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
@jumbosushi jumbosushi changed the title Support multiple toolchains in py3_image Support python versions in py3_image's py_binary Apr 3, 2023
@jumbosushi jumbosushi changed the title Support python versions in py3_image's py_binary Support multiple python versions in py3_image's py_binary Apr 3, 2023
@github-actions
Copy link

github-actions bot commented Oct 1, 2023

This issue has been automatically marked as stale because it has not had any activity for 180 days. It will be closed if no further activity occurs in 30 days.
Collaborators can add an assignee to keep this open indefinitely. Thanks for your contributions to rules_docker!

@github-actions github-actions bot added the Can Close? Will close in 30 days unless there is a comment indicating why not label Oct 1, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Can Close? Will close in 30 days unless there is a comment indicating why not
Projects
None yet
Development

No branches or pull requests

1 participant