我想使用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?
发布于 2019-11-23 07:40:48
下面的代码将在proto目录中生成.proto文件所在的python文件。
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
])我从答案中得到了一个提示:https://stackoverflow.com/a/54236739/361100
发布于 2020-03-26 21:25:37
如果您正在遵循关于golang的媒体教程,不复制和粘贴:
protoc --go_out=. *.proto
命令,因为--不是真正的-(破折号等),所以手工键入命令解决了我的问题。
由于复制和粘贴不正确的字符,我收到此错误。
我希望你能从我愚蠢的错误中吸取教训:)
https://stackoverflow.com/questions/57868551
复制相似问题