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>
?