Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python protobuf namespacing #973

Closed
mightyguava opened this issue Nov 12, 2015 · 3 comments
Closed

Python protobuf namespacing #973

mightyguava opened this issue Nov 12, 2015 · 3 comments

Comments

@mightyguava
Copy link

Hi,

I'm trying to integrate protobufs with an existing codebase that's structured like

src/
  python/
  java/
  thrift/

And I'm planning to add a src/proto3 directory. It's a bit lost as to how to make this work for the Python code since "In Python, the package directive is ignored, since Python modules are organized according to their location in the file system."

So if I wanted to generate them in a folder like /gen/proto3/python/, where the python folder is placed on the $PYTHON_PATH, a thrift file placed at src/proto3/demo/demo.proto would be turned into /gen/proto3/python/demo/demo_pb2.py (assuming I set -I src/proto3). I would have to generate __init__.py manually, which is fine, but its module path is demo.demo_pb2 rather than something like gen.proto3.demo.demo_pb2, so it'll easily run into module conflicts with stuff in src/python.

Of course I could move the protos to src/proto3/gen/proto3 instead and it'll give the desired namespace, but that's kind of... weird?

It seems like the generated code are designed to be put in the same module that the code that references them will be in, which seems like it'd require building some non-trivial tooling around so it's not cluttering the source tree.

So to summarize, I think I'm probably missing something really simple, that there's a much better way to organize this? If not, might there be an option for option python_package to specify custom module paths for Python code as well?

Thanks!

@awan1
Copy link

awan1 commented Mar 3, 2016

I'm not 100% sure, but I think issue #881 might be related.

@liujisi
Copy link
Contributor

liujisi commented Mar 6, 2017

There's no way to set python package. The easiest workaround would be copy the source or generated protos.

@liujisi liujisi closed this as completed Mar 6, 2017
@thomasmckay
Copy link

Is there really no way to get protobuf bindings for python? This repo cannot be converted:
https://github.com/googleapis/googleapis

TypeError: Couldn't build proto file into descriptor pool!
Invalid proto descriptor for file "annotations.proto":
  annotations.proto: Import "google/api/http.proto" has not been loaded.
  google.api.http: "google.api.HttpRule" seems to be defined in "http.proto", which is not imported by "annotations.proto".  To use it here, please add the necessary import.

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

No branches or pull requests

5 participants