1

I want to generate protobuf (pb2) files for Python programmatically using grpc_tools.

With this layout (potentially other languages in parallel to 'python/')

+---gen
+---protos
|       phone.proto
|
\---python
        call_protoc.py

this installation in a (Windows) Python 3.9.5 venv

grpcio==1.38.0
grpcio-tools==1.38.0
protobuf==3.17.0

and this very short call_protoc.py

from grpc_tools import protoc

protoc.main('--proto_path=../protos --python_out=../gen --grpc_python_out=../gen phone.proto'.split())

calling in ./python (no separate protoc installation)

python -m grpc_tools.protoc --proto_path=../protos --python_out=../gen --grpc_python_out=../gen phone.proto

generates

+---gen
    phone_pb2.py
    phone_pb2_grpc.py

but calling (same directory, same parameters)

python call_protoc.py

tells: Could not make proto path relative: phone.proto: No such file or directory

From looking into the implementation I found it prepends sys.argv[0] and appends a -I<site-packages>/grpc_tools/_proto.py. There is no reason given for these extra parameters.

So I need to prepend any single parameter, then protoc.main() works as expected

from grpc_tools import protoc

protoc.main('foo_or_just_a_dot --proto_path=../protos --python_out=../gen --grpc_python_out=../gen phone.proto'.split())

Did I miss something in the documentation of grpc_tools.protoc.main()?

Shouldn't it work same as calling

python -m grpc_tools.protoc <normal_protoc_arguments>

?

thoku
  • 1,120
  • 9
  • 27
  • Can you try referencing your proto with its import path prefixed? i.e. `../protos/phone.proto` – DazWilkin May 22 '21 at 13:47
  • Thanks for looking into that, but this path for the proto is given already with ``--proto_path=`` (aka ``-I``). So this does not remove the need for the strange leading extra (dummy) parameter, like just a dot. (I provided all information, besides I used for this minimal example this proto: https://github.com/grpc/grpc/blob/master/examples/python/async_streaming/phone.proto but this does not matter for the question.) I would expect ``protoc.main()`` should be called with the same parameters as ``python -m grpc_tools.protoc`` (or protoc, which I did not install). – thoku May 22 '21 at 16:08
  • Thanks for asking and providing the workaround with the arbitrary single parameter, made my day. – Dominik Apr 13 '23 at 20:16

0 Answers0