Skip to content

Python Namespace not handling correctly #6844

@lidizheng

Description

@lidizheng

Description of the problem

The order of Python import will affect the build result. I'm using protobuf and googleapis-common-protos. Both of them are under google. package prefix. googleapis-common-protos includes multiple generated Python proto files. If I import the googleapis-common-protos before protobuf, the build will fail with ImportError. But if I switch those two, Bazel will be happy.

It seems Bazel has some bug with Python Namespace Package PEP 420

Bugs: what's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.

The way of reproduce is simple, build the files within virtualenv. And the bug will appear.

$ python2.7 -m virtualenv env
$ source env/bin/activate
$ bazel run happy
$ bazel run sad
# Module missing
load("@my_dep//:requirements.bzl", "requirement")

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

py_binary(
    name = "happy",
    srcs = ["happy.py",],
    deps = [
        requirement('six'),
        requirement('protobuf'),
        requirement('googleapis-common-protos'),
    ],
)

py_binary(
    name = "sad",
    srcs = ["sad.py",],
    deps = [
        requirement('six'),
        requirement('protobuf'),
        requirement('googleapis-common-protos'),
    ],
)

# WORKSPACE
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")

git_repository(
    name = "io_bazel_rules_python",
    commit = "8b5d0683a7d878b28fffe464779c8a53659fc645",
    remote = "https://github.com/bazelbuild/rules_python.git",
)

load("@io_bazel_rules_python//python:pip.bzl", "pip_repositories", "pip_import")

pip_repositories()

pip_import(
    name = "my_dep",
    requirements = "//:requirements.txt",
)

load("@my_dep//:requirements.bzl", "pip_install")

pip_install()
# happy.py
import google.protobuf
from google.rpc import status_pb2
# sad.py
from google.rpc import status_pb2
# requirements.txt
six
protobuf
googleapis-common-protos

What operating system are you running Bazel on?

macOS 10.13.6

What's the output of bazel info release?

release 0.20.0

Any other information, logs, or outputs that you want to share?

What I see as output:

INFO: Invocation ID: 4aa95efc-3d7d-474b-b19c-ede82d4a420e
INFO: Analysed target //:sad (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
Target //:sad up-to-date:
  bazel-bin/sad
INFO: Elapsed time: 0.114s, Critical Path: 0.00s
INFO: 0 processes.
INFO: Build completed successfully, 1 total action
INFO: Build completed successfully, 1 total action
Traceback (most recent call last):
  File "/private/var/tmp/_bazel_lidiz/ed80de51ffd393df639cb4de782ff244/execroot/__main__/bazel-out/darwin-fastbuild/bin/sad.runfiles/__main__/sad.py", line 1, in <module>
    from google.rpc import status_pb2
ImportError: No module named rpc

Metadata

Metadata

Assignees

No one assigned

    Labels

    P4This is either out of scope or we don't have bandwidth to review a PR. (No assignee)staleIssues or PRs that are stale (no activity for 30 days)team-Rules-PythonNative rules for Pythontype: bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions