0

I'm maintaining a project that is using Bazel 0.5.4, and it needs to run on an environment where python does not exist. I'd like to point the application to using python3.

This issue is similar to Bazel 0.26.1 use Python3 on py_test, but the version of rules_python mentioned in that ticket is so new that it's not compatible with bazel 0.5.4

GOAL: Issue python commands with /usr/bin/python3, not python

As-is, the application fails when it's unable to find python bazel build completes without an issue, but bazel test continues to look for python, and fail.

bazel test ... --test_output=all --python_top=//sandbox_app:python-3.8.10
...
/usr/bin/env: 'python': No such file or directory
================================================================================
INFO: Elapsed time: 0.658s, Critical Path: 0.10s
INFO: Build completed, 1 test FAILED, 2 total actions
//sandbox_app:sandbox_test                                               FAILED in 0.1s

This didn't work

The following configuration worked for a similar user, described here: https://groups.google.com/g/bazel-discuss/c/nVQ48R94S_8 But even after a bazel clean, bazel still continues to invoke python when running the tests.

WORKSPACE

git_repository(
    name = "io_bazel_rules_go",
    remote = "https://github.com/bazelbuild/rules_go.git",
    tag = "0.5.4",
)

BUILD.bazel

package(default_visibility = ["//visibility:public"])

py_test(
    name = "sandbox_test",
    srcs = ["sandbox_test.py"],

    default_python_version = "PY3",
    srcs_version = "PY3",
)

py_runtime(
    name = "python-3.8.10",
    files = [],
    interpreter_path = "/usr/bin/python3",
)

Version details:

$ bazel info release
release 0.5.4

It would minimize disruption to the application if this can be done without upgrading bazel.

sandbox_test.py

#!/usr/bin/python3

import unittest

class SandboxTest(unittest.TestCase):

  def testRunSandbox(self):
    # Valid for Python 3, syntax error for python2
    print(print("Hello, Python!"))


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

Possibly related issues

https://github.com/bazelbuild/bazel/issues/4815 https://github.com/bazelbuild/bazel/issues/200

Rules spec:

https://bazel.build/reference/be/python

Similar issues:

Val H
  • 507
  • 4
  • 13
  • Tool chains have been the acceptable way to shim in python3: https://stackoverflow.com/questions/67512066/how-do-i-select-the-runtime-in-bazel-for-python-and-pip But seem to require toolchains. A related question is not whether or not toolchains with bazel release 0.5.4 are an option. https://stackoverflow.com/questions/73751276/toolchains-with-bazel-0-5-4 – Val H Sep 17 '22 at 00:03

0 Answers0