Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop bazel from relying on Python2

Tags:

python

bazel

when running bazel test Bazel seems to default to Python 2 even when --python-version flag is specified

bazel test //... --python_version PY3
INFO: From Testing //test:py-unit-tests:
==================== Test output for //test:py-unit-tests:
/usr/bin/env: 'python': No such file or directory

This is my BUILD file

py_test(
    name = "py-unit-tests",
    srcs = glob(["unit/**/*.py"]),
    deps = [
    ],
    main = "unit/unit_test_runner.py",
    timeout = "short",
)

And the test file

import sys
import unittest


class TestGeneration(unittest.TestCase):
    def test_base(self):
        pass
    def test_urdf(self):
        self.assertEqual("hello", 'test')


if __name__ == '__main__':
    unittest.main()

Bazel version: 3.3.1


Other notable things:

  1. My system has both py2 and py3 installed

  2. Py3 is located at /usr/bin/python3

  3. Py2 is located at /usr/bin/python2

There is no /usr/bin/python

like image 917
cjds Avatar asked Jul 08 '20 19:07

cjds


People also ask

Does Bazel need Python?

Start by installing Bazel. To build our Flask application, we need to instruct Bazel to use python 3.8. 3 hermetically. This means that we can't rely on the Python version installed on the host machine -- we must compile it from scratch!

What does Bazel clean -- expunge do?

Bazel expunge will remove all downloaded artifacts.

What is Starlark language?

Starlark is a dialect of Python. Like Python, it is a dynamically typed language with high-level data types, first-class functions with lexical scope, and garbage collection. Independent Starlark threads execute in parallel, so Starlark workloads scale well on parallel machines.

How does Bazel run work?

When you run bazel , you're running the client. The client finds the server based on the output base, which by default is determined by the path of the base workspace directory and your userid, so if you build in multiple workspaces, you'll have multiple output bases and thus multiple Bazel server processes.


1 Answers

As described in https://github.com/bazelbuild/bazel/issues/11554, the issue is that your Bazel python stub has a #!/usr/bin/env python shebang. If python is not on the PATH like in your case, the stub will fail.

Previously the only solution would be to add a symlink from python to python3, e.g. by installing

sudo apt install python-is-python3

However since https://github.com/bazelbuild/bazel/commit/763dd0ce6e1644bf895231432f616427a11d385a has landed that stub shebang has become configurable. You can now define your own py_runtime (https://docs.bazel.build/versions/main/be/python.html#py_runtime)

Alternatively, since https://github.com/bazelbuild/bazel/commit/2945ef5072f659878dfd88b421c7b80aa4fb6c80 the default shebang has also become #!/usr/bin/env python3

These two commits are only available from Bazel 5.0.0 though, so for now you might have to build Bazel yourself to get them.

like image 143
Laurenz Avatar answered Nov 15 '22 01:11

Laurenz