2

I'd like to use protoc.main(...) in order to generate python gRPC files during runtime as below.

from grpc_tools import protoc
args = "--proto_path=. --python_out=grpc --grpc_python_out=grpc alpha.proto"
result = protoc.main(c)

The above code gives 'missing output directives' error with result code 1.

However, below workaround to put alpha.proto as a commandline args works. It means alpha.proto file is fine.

import subprocess
result = subprocess.call("python -m grpc_tools.protoc " + args, shell=True)

The possible causes are (1) protoc.main does encode to each character as below code, or (2) protoc.main wrongly resolves argument paths internally.

def main(command_arguments):
    """Run the protocol buffer compiler with the given command-line arguments.

  Args:
    command_arguments: a list of strings representing command line arguments to
        `protoc`.
  """
    command_arguments = [argument.encode() for argument in command_arguments]
    return _protoc_compiler.run_main(command_arguments)

How can I use protoc.main correctly?

Youngjae
  • 24,352
  • 18
  • 113
  • 198

2 Answers2

2

Below code will generate python files in proto directory where .proto files reside.

proto_files = ["proto/file1.proto", "proto/file2.proto"]

from grpc_tools import protoc
for file in proto_files:
    grpc_tools.protoc.main([
        'grpc_tools.protoc',
        '--proto_path=.',
        '--python_out=.',
        '--grpc_python_out=.',
        file
    ])

I've got a hint from the answer; https://stackoverflow.com/a/54236739/361100

Youngjae
  • 24,352
  • 18
  • 113
  • 198
0

If you are following the medium tutorial for golang protobufs, DO NOT COPY AND PASTE the:

protoc --go_out=. *.proto

command, because the -- are not real -'s (dash, etc.), so hand typing the command fixed my problem.

I received this error due to copy and pasting improper characters

I hope you learn from my silly mistake :)

jenkizenki
  • 741
  • 6
  • 14