Skip to content

Add includes/imports attribute to Python rules #702

Closed

Description

@davidzchen

It would be great if the py_* rules had an includes or imports attribute, similar to the includes attribute for the cc_* rules, that would add the specified directory to PYTHONPATH for all targets that depend on the target.

For example, currently, the BUILD file for protobuf currently works around the lack of such an attribute by having a custom genrule copy the sources for Python support. Further, vendoring protobuf as a third_party library for Python support requires importing protobuf at //google/protobuf due to this limitation.

Activity

  1. added
    P2
    on Dec 15, 2015
  2. removed their assignment
    on Dec 15, 2015
  3. added
    P3
    and removed
    P2
    on Dec 15, 2015
  4. davidzchen commented on Jan 22, 2016

    @davidzchen
    MemberAuthor

    After looking further into how to use protobuf for Python with Bazel, I am now more convinced that we need this in order to remove the hack that requires importing the protobuf repo as //google/protobuf in your workspace.

    As explained in the above link, the reason why need this hack is because the generated Python protobuf files contain imports that look like:

    from google.protobuf.xxx import yyy

    Currently, Bazel's Python rules require importing from the workspace root, and there is no way to add additional directories to the PYTHONPATH. Including the protobuf repo as an external repository will not work either because there is no //google package in the protobuf repo.

  5. damienmg commented on Jan 22, 2016

    @damienmg
    Contributor

    Once protocolbuffers/protobuf#1165 is merged, we can indeed recommends to remove the comment.

  6. davidzchen commented on Jan 22, 2016

    @davidzchen
    MemberAuthor

    Doesn't protocolbuffers/protobuf#1165 only fix the problem encountered when loading protobuf.bzl from an external repository? Or does it also solve the Python imports problem?

  7. damienmg commented on Jan 22, 2016

    @damienmg
    Contributor

    It doesn't solve the py import problem as a submodule but PYTHONPATH is
    correctly set to include external repositories so it fixes those case.

    On Fri, Jan 22, 2016 at 11:40 AM David Z. Chen [email protected]
    wrote:

    Doesn't protocolbuffers/protobuf#1165
    protocolbuffers/protobuf#1165 only fix the problem
    encountered when loading protobuf.bzl from an external repository? Or does
    it also solve the Python imports problem?

    —
    Reply to this email directly or view it on GitHub
    #702 (comment).

  8. davidzchen commented on Jan 22, 2016

    @davidzchen
    MemberAuthor

    I still don't quite follow. The generated sources still from google.protobuf.xxx import yyy.

    • In the git submodule case, this currently requires the the protobuf repository to be under //google/protobuf in the workspace.
    • In the external repository case, from what I understand the root of the external repository is added to PYTHONPATH but there is no google/ directory in the protobuf source tree.
  9. damienmg commented on Jan 22, 2016

    @damienmg
    Contributor

    oh I see, yes it's a good idea to upstream those change too.

  10. davidzchen commented on Jan 24, 2016

    @davidzchen
    MemberAuthor

    I have a patch that adds the imports attribute to the Python rules. It works with the Python example under //examples/py_native. Once I successfully test it with protobuf and having tensorflow include protobuf as an external repository, I'll send it out for review.

  11. damienmg commented on Jan 26, 2016

    @damienmg
    Contributor

    I rethough a bit of

      In the external repository case, from what I understand the root of the external repository
      is added to PYTHONPATH but there is no google/ directory in the protobuf source tree.
    

    When including a git_repository and extracting the submodules (for TensorFlow for example), it works just fine because the PYTHONPATH include the git repository base and the submodule for protobuf will be in google/protobuf.

    Anyway, we definitely wants to have your change in for 0.2 I think, could try to merge it ASAP?

  12. added
    P1
    and removed
    P3
    on Jan 26, 2016
  13. added this to the 0.2 milestone on Jan 26, 2016
  14. davidzchen commented on Jan 27, 2016

    @davidzchen
    MemberAuthor

    Agreed. I'll send it out for review as soon as I am done testing it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P1type: feature request

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions