Search code examples
pythonbazelbazel-python

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


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?


Solution

  • 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'
    )