我想用于protoc.main(...)在运行时生成 python gRPC 文件,如下所示。from grpc_tools import protocargs = "--proto_path=. --python_out=grpc --grpc_python_out=grpc alpha.proto"result = protoc.main(c)上面的代码给出了带有结果代码的“缺少输出指令”错误1。但是,下面的解决方法可以alpha.proto作为命令行 args 工作。这意味着alpha.proto文件很好。import subprocessresult = 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正确使用?
2 回答

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

暮色呼如
TA贡献1853条经验 获得超9个赞
proto下面的代码将在文件所在的目录中生成 python 文件.proto。
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
])
添加回答
举报
0/150
提交
取消