Unrelated to Pants. Related to gRPC and `protoc` f...
# random
b
Unrelated to Pants. Related to gRPC and
protoc
for python: How can I have protoc make this:
Copy code
# Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT!
"""Client and server classes corresponding to protobuf-defined services."""
import grpc
from . import rdu_pb2 as rdu__pb2  <-- Relative import!
...
instead of
Copy code
# Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT! 
"""Client and server classes corresponding to protobuf-defined services.""" 
import grpc 
import rdu_pb2 as rdu__pb2   <--- fails!
Have looked around on the internet and it seems most suggestions involve adding this generated protobuf folder to the $PYTHONPATH. Since I'd rather not do this I'm coming here for some advice!
b
Dumb idea, but could you just post-process the
protoc
output to patch in the relative import using
sed
? That's probably what I would do
👌 1
b
I've seen this online too.
Just wondering if there's a better way. Seems like protoc should be more smart about Python bindings
g
Just had to go check, in our pre-Pants repo we do the following fixups:
Copy code
sed -i -E 's/^import.*_pb2/from . \0/' $app/api/*.py
sed -i -E "s/'__module__' : '/'__module__' : '$app.api./g" $app/api/*.py
touch ./$app/api/__init__.py
So both fixing imports, module names, and adding a init.py. Had completely forgotten these hacks were even necessary 😄
b
Okay then. I accept defeat!
l
Probably you seen it but there is also https://github.com/cpcloud/protoletariat
b
I had not yet seen it. Thanks for the recommendation.