python3 with the default rules?

1,212 views
Skip to first unread message

macrae...@gmail.com

unread,
Sep 26, 2017, 7:27:32 PM9/26/17
to bazel-discuss
Is it actually possible to use python3 with bazel currently? I have a very small starting case and it always runs python 2.

here's my setup:

WORSPACE
```
git_repository(
name = "io_bazel_rules_python",
remote = "https://github.com/bazelbuild/rules_python.git",
commit = "fa77c9c1118380e066c88b955c90fb3c7353429e",
)

# I'm trying to use the default python rules because I want to be able to use the new pip-install rules
```

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

## smoketest
py_binary(
name = "smoketest",
default_python_version = "PY3",
srcs_version = "PY3ONLY",
srcs = ["smoketest.py"],
)
```

This happily compiles and runs, but even though everything says PY3, it's running python2.

Is there a way to tell the py_binary to use the py_runtime that was created above it? deps= didn't work.

The docs say that srcs_version has been deprecated and "no longer has effect" but before I added that in, I got an error from 2to3.


Unless there's an easy way I'm probably just going to go back to using the pex_binary rule, because that works even though it doesn't cache pip dependencies.

macrae...@gmail.com

unread,
Sep 26, 2017, 10:36:00 PM9/26/17
to bazel-discuss
This is with bazel 0.5.4 on an ubuntu image.

I would really love to figure out how to make this work. bazel_pex has its own problems.

Benjamin Staffin

unread,
Sep 27, 2017, 5:07:47 PM9/27/17
to bazel-discuss
On Tuesday, September 26, 2017 at 10:36:00 PM UTC-4, macrae...@gmail.com wrote:
This is with bazel 0.5.4 on an ubuntu image.

I would really love to figure out how to make this work. bazel_pex has its own problems.


I haven't tested it, but I think `bazel build --python_top=//your/package:python-3.5.2` might be what you want:

  --python_top (a build target label; default: see description)
    The label of py_runtime rule used for the Python interpreter invoked by 
    Bazel.

Austin Schuh

unread,
Sep 29, 2017, 1:44:09 AM9/29/17
to Benjamin Staffin, bazel-discuss
--python_top successfully switched which version of python is in use for me.

I haven't found a way to select the python version at the target level though.  Some of our code is python2 and some is python3.  Both versions need to be used in the same build.  How can we pick which python version is in use at the target?

I'd love to convert everything over to python3, but that both needs to be an incremental process, and not all libraries support python3.

Thanks,
  Austin

--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/c63282a5-6832-4451-85a0-0bb5af3bf9dc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Greg Estren

unread,
Oct 6, 2017, 3:39:18 PM10/6/17
to bazel-discuss
Hi Austin and everyone,

This is a well-timed question.

There's a long history here, but the basic answer is you can't choose the Python version at the target level (srcs_version, default_python_version): it has to be set for the entire build (--python_top)

The good news is the Python and Bazel teams have been actively collaborating recently to upgrade this. We already have code in review which will make the above possible, and should be committed upstream in a week or two. This is currently Google-focused: we may require a few more tweaks to fully opt Bazel in too. But those tweaks should be small and I'm more than happy to make sure Bazel is at feature parity with this.

I'll file a tracking bug to follow.

Greg Estren

unread,
Oct 6, 2017, 3:42:07 PM10/6/17
to bazel-discuss

Austin Schuh

unread,
Oct 6, 2017, 6:24:01 PM10/6/17
to Greg Estren, bazel-discuss
Thanks Greg!  I'm now following the bug and look forwards to a real solution.

If anyone is interested in the meantime, I've got a patch which adds python from the py_runtime to the runfiles.  I was able to then point python at a shell script which reads the shebang line out of the top of the file and calls the right python from there.

Austin

Austin Hendrix

unread,
Jul 30, 2018, 10:02:10 PM7/30/18
to bazel-discuss
Hi other Austin! Is there any chance you could share your script for choosing the correct python version?

Thanks,
-Austin

Austin Schuh

unread,
Jul 30, 2018, 11:16:47 PM7/30/18
to Austin Hendrix, bazel-discuss
Hi Austin!

It's embarrassingly simple.

if head -n 1 "$1" | grep -q python3; then
  exec "$BASE_PATH"/usr/bin/python3 "$@"
else
  exec "$BASE_PATH"/usr/bin/python2 "$@"
fi

Happy pythoning!
  Other Austin

Reply all
Reply to author
Forward
0 new messages