How do I select the runtime in bazel for Python and pip?

7.5k views Asked by At

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?

3

There are 3 answers

0
Benjamin Peterson On BEST ANSWER

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'
)
1
slsy On

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

0
sierikov On

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.