grpcio-tools protoc.main 给出“缺少输出指令”错误

我想用于protoc.main(...)在运行时生成 python gRPC 文件,如下所示。


from grpc_tools import protoc

args = "--proto_path=. --python_out=grpc --grpc_python_out=grpc alpha.proto"

result = protoc.main(c)

上面的代码给出了带有结果代码的“缺少输出指令”错误1。


但是,下面的解决方法可以alpha.proto作为命令行 args 工作。这意味着alpha.proto文件很好。


import subprocess

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

可能的原因是 (1)protoc.main确实像下面的代码那样对每个字符进行编码,或者 (2) 在内部protoc.main错误地解析了参数路径。


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)

如何protoc.main正确使用?


森林海
浏览 298回答 2
2回答

一只萌萌小番薯

如果您正在遵循 golang protobufs 的中等教程,请不要复制和粘贴:protoc --go_out=. *.proto命令,因为--不是真实-的(破折号等),所以手动输入命令解决了我的问题。由于复制和粘贴不正确的字符,我收到此错误我希望你能从我的愚蠢错误中吸取教训:)

暮色呼如

proto下面的代码将在文件所在的目录中生成 python 文件.proto。proto_files = ["proto/file1.proto", "proto/file2.proto"]from grpc_tools import protocfor file in proto_files:    grpc_tools.protoc.main([        'grpc_tools.protoc',        '--proto_path=.',        '--python_out=.',        '--grpc_python_out=.',        file    ])
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python