Im trying to build python 3 image and it is failing due to:
_Is there something Im missing?_
Command used to build
bazel build //services:server_image --force_python=py3 --python_path=/Users/bazel_user/.virtualenvs/p3-opt/bin/python
ModuleNotFoundError: No module named 'urlparse'
In my WORKSPACE i have defined
git_repository(
name = "io_bazel_rules_docker",
commit = "3caddbe7f75fde6afb2e2c63654b5bbeeeedf2ac",
remote = "https://github.com/bazelbuild/rules_docker.git",
)
git_repository(
name = "io_bazel_rules_python",
commit = "73a154a181a53ee9e021668918f8a5bfacbf3b43",
remote = "https://github.com/bazelbuild/rules_python.git",
)
load(
"@io_bazel_rules_docker//python3:image.bzl",
_py_image_repos = "repositories",
)
_py_image_repos()
And in my BUILD file I have the following
load("@io_bazel_rules_docker//python3:image.bzl", "py3_image")
py3_image(
name = "server_image",
srcs = ["server.py"],
default_python_version = "PY3",
main = "server.py",
srcs_version = "PY3",
visibility = ["//visibility:public"],
deps = [
":server_deps"
],
)
Error stack trace
BUILD:94:1: no such package '@py3_image_base//image': Pull command failed: Traceback (most recent call last):
File "/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/private/var/tmp/_bazel_user/4c04a98a46a7b3fac47c7e4e88c0a736/external/puller/file/puller.par/__main__.py", line 29, in <module>
File "/private/var/tmp/_bazel_user/4c04a98a46a7b3fac47c7e4e88c0a736/external/puller/file/puller.par/containerregistry/client/__init__.py", line 19, in <module>
File "/private/var/tmp/_bazel_user/4c04a98a46a7b3fac47c7e4e88c0a736/external/puller/file/puller.par/containerregistry/client/docker_name_.py", line 21, in <module>
ModuleNotFoundError: No module named 'urlparse'
That's a python 3 bug in the google/containerregistry repo:
https://github.com/google/containerregistry/blob/master/client/docker_name_.py#L21
The right solution there is to use six to get Python 2/3 compatibility, but it's probably not the last problem you'll encounter (e.g. google/containerregistry#42)
Ah I see ok, Ill pay attention to updates to this repo and retry the py3_image when these issues are resolved.
Thanks.
Is there perhaps a workaround to configure bazel to use python2 when using the container registry?
I am having the same issue and tried to pass several flags to bazel, but none of them seem to make this work. The only workaround that I have found so far is to change the symlink from /usr/bin/python -> /usr/bin/python3 to /usr/bin/python -> /usr/bin/python2, but that is not ideal.
If anyone is still having issues with this, I managed to get Bazel to use python2 here by setting BAZEL_PYTHON=python2.7, as used here: https://github.com/bazelbuild/rules_docker/blob/master/container/pull.bzl#L22
I don't know how well supported this environment variable is though.
@gnatpat Unfortunately that does not work for me
Any update to this issue? py3_image is not usable because of this bug.
We've started migrating https://github.com/google/containerregistry to python3 internally. I can't give any time estimates, but someone is actively working on it now.
Example workaround with python_path thats working for us:
bazel.rc:
build --python_path=/path/to/py3_wrapper.sh
py3_wrapper.sh:
#!/bin/bash
if [[ $1 = *"/bazel_tools/"* ]] || \
[[ $1 = *"/containerregistry/"* ]] || \
[[ $1 = *"/io_bazel_rules_docker/"* ]]; then
PYTHON_BIN=/usr/bin/python2
else
PYTHON_BIN=/usr/bin/python3
${PYTHON_BIN} "$@"
Most helpful comment
Example workaround with python_path thats working for us:
bazel.rc:
py3_wrapper.sh: