You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add includes/imports attribute to Python rules #702
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.
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:
fromgoogle.protobuf.xxximportyyy
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.
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?
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.
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.
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?
Activity
davidzchen commentedon Jan 22, 2016
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/protobufin 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:
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//googlepackage in the protobuf repo.damienmg commentedon Jan 22, 2016
Once protocolbuffers/protobuf#1165 is merged, we can indeed recommends to remove the comment.
davidzchen commentedon Jan 22, 2016
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?
damienmg commentedon Jan 22, 2016
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:
davidzchen commentedon Jan 22, 2016
I still don't quite follow. The generated sources still
from google.protobuf.xxx import yyy.//google/protobufin the workspace.PYTHONPATHbut there is nogoogle/directory in the protobuf source tree.damienmg commentedon Jan 22, 2016
oh I see, yes it's a good idea to upstream those change too.
Add `imports` attribute to Python native rules.
davidzchen commentedon Jan 24, 2016
I have a patch that adds the
importsattribute 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.Add `imports` attribute to Python native rules.
damienmg commentedon Jan 26, 2016
I rethough a bit of
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?
davidzchen commentedon Jan 27, 2016
Agreed. I'll send it out for review as soon as I am done testing it.
Add `imports` attribute to Python native rules.
Add `imports` attribute to Python native rules.