5

I have a large and diverse codebase, much of which is python(3), which I am trying to organise with bazel.

I would ideally like (in approximate order of importance):

  1. To default to python3 (ideally a specific version, but I can live with just using whatever's in /usr/bin/python3)
  2. To send options to the python interpreter, ideally in a per-target manner
  3. The option to override the python interpreter and other options for specific targets

Right now I have a partial solution and I would like some advice on whether it is idiomatic and where it might be improved.

In /BUILD I have

genrule(
  name = "python3_interpreter",
  outs = ["python"],
  cmd = "echo '#!/bin/bash\n/usr/bin/python3 -OO \"$$@\"\n' > $@",
  executable = True,
)
py_runtime(
  name = "python3",
  visibility = ["//visibility:public"],
  interpreter = ":python3_interpreter",
  files = []
)

to create a man-in-the-middle python interpreter, where I can add options to the python interpreter (-OO in this case).

In /.bazelrc I have

build --python_top=//:python3
run --python_top=//:python3

To default to that interpreter.

I could probably use a macro around the genrule to make adding interpreter options more ergonomic (and perhaps search for python3 in a more structured way), but it still feels a lot like a hack.

I could also use an external repository to download and build a specific python version from source, more effort but also more hermitic.

None of these options seem particularly satisfying and none allow me to override things per-target.

Any advice or constructive criticism would be welcome. I'm already using rules_python to manage pip dependencies. I'm also thinking of using rules_pyz, which looks like it might be easier to edit to support other features I need from python (e.g. py_library that depends on cc_library).

JMAA
  • 1,730
  • 13
  • 24

0 Answers0