Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Way to use different python versions for different Bazel rules

Tags:

python

bazel

I am able to get a py_runtime to point to the correct python interpreter, but now I want to be able to use python 3 for my Bazel rules, and python 2 for existing Bazel rules that were written for python 2. Given py_runtime below, how do I add it to the py_library so that it runs on python 3 while everything else runs on python 2?

py_runtime(
    name = "python-3.6",
    files = [],
    interpreter = "python3interpreter",
)

py_runtime(
        name = "python-2.7",
        files = [],
        interpreter = "python2interpreter",
    )

py_library(
    name = "foo",
    srcs = ["foo.py"]
)
like image 314
Zeitgeist Avatar asked Jun 21 '26 22:06

Zeitgeist


1 Answers

First of all use py_runtime_pair to define both PY2 and PY3 toolchains. Then note that only py_binary has python_version attached to it, not the libraries. That is because a library can be PY2 and PY3 compatible. Although for a py_library you must define deps correctly. You must use select on python_version to pick up correct deps.

like image 52
ash Avatar answered Jun 23 '26 21:06

ash