4

I'm trying to build an app on Ubuntu 20.04, where python3 points to Python3.8, and I'm building aganist Python3.6

I have the following runtime in the same directory of WORKSPACE.

$ cat BUILD.bazel 
py_runtime(
    name = "python3.6",
    interpreter_path = "/usr/bin/python3.6",
)

I tried to build the app by running the following and bazel still points to python3 which is python3.8

bazelisk build company/app_api:app --python_top=//:python3.6

I also tried the deprecated option and didn't work either.

bazelisk build company/app_api:app --python_path=/usr/bin/python3.6

This is the error I get:

...
subprocess.CalledProcessError: Command '['/usr/bin/python3', '-m', 'pip', '--isolated', 'wheel', '-r', '/source_code/src/python/third_party/requirements.txt']' returned non-zero exit status 1.
...

pip is trying to install a package that works only with python3.6, and that's why it's returning a non zero exist code.

How do I force bazel to use a custom python interpreter?

Ahmed
  • 2,825
  • 1
  • 25
  • 39

3 Answers3

2

py_runtime usually must be used with py_runtime_pair and toolchain . See the example in the py_runtime_pair documentation. That example, slightly modified to apply to the OP would look like:

load("@bazel_tools//tools/python:toolchain.bzl", "py_runtime_pair")

py_runtime(
    name = "python3.6",
    interpreter_path = "/usr/bin/python3.6",
    python_version = "PY3",   
)

py_runtime_pair(
    name = "py3.6",
    py3_runtime = ":python3.6",
)

toolchain(
    name = "py3-tc",
    toolchain = ":py3.6",
    toolchain_type = "@bazel_tools//tools/python:toolchain_type",
)

Then one can use the new toolchain by placing register_toolchains("//path/to/python3:py3-tc") in the WORKSPACE file or passing the --extra_toolchains //path/to/python3:py3-tc command line flag.

Add python_interpreter to pip_install in WORKSPACE.

pip_install(
    requirements = "//third_party:requirements.txt",
    python_interpreter = 'python3.6'
)
Ahmed
  • 2,825
  • 1
  • 25
  • 39
Benjamin Peterson
  • 19,297
  • 6
  • 32
  • 39
0

If you are using pip support from a bazelbuild/rules_python, then there is no probably a clean way to do this like reusing a python interpreter from the toolchain. python3 is hardcoded in the rule definition

slsy
  • 1,257
  • 8
  • 21
  • 1
    It isn't hardcoded. This is the default value for the `version` variable. And as described in the [Bazel docs](https://docs.bazel.build/versions/master/be/python.html#py_runtime) and [docs of python rules](https://github.com/bazelbuild/rules_python/blob/master/docs/python.md) you can redefine the interpreter version. – sierikov May 13 '21 at 23:26
0

Following to the documentation following example should work with python version 3.6.0. I think bazel just doesn't follow symlinks and falls back to a system-default version of python.

py_runtime(
    name = "python-3.6.0",
    interpreter_path = "/opt/pyenv/versions/3.6.0/bin/python",
)

You can also redefine python toolchain as described here.

sierikov
  • 121
  • 1
  • 6