How do I use grpcio-tools/ grpc_tools.protoc for relative imports in proto file

Viewed 1922

Say you have your dependent external interfaces for a service called crane under \interaces\crane folder like below

--interfaces
-----crane
------- crane.proto
------- vehicle.proto

And in crane.proto which is an external proto, you have this relative reference to vehicle proto

import crane/vehicle.proto
....

How do I use grpc_tools to generate the proto buffers for python binding correctly?

Using the below gives an error in crane.proto that vehicles.proto is not found

$(PYTHON) -m grpc_tools.protoc -I ./interfaces/crane --python_out=./my_proj/generated -- \
grpc_python_out=./my_proj/generated crane.proto
1 Answers

You can use like below https://github.com/grpc/grpc/issues/9575#issuecomment-293934506

build:

$(MKDIR) -p myproj/generated
touch myproj/generated/__init__.py
$(MKDIR) -p ndn_objtrack/myproj/crane
touch myproj/generated/crane/__init__.py

$(PYTHON) -m grpc_tools.protoc -I ./interfaces/ --python_out=./my_proj/generated -- \
grpc_python_out=./my_proj/generated crane/crane.proto

And to use in your Python file

import grpc

from generated.myproj import crane_pb2
from generated.myproj import crane_pb2_grpc
Related