Python protobuf gRPC生成了一个不存在的依赖。

14

我正在尝试通过以下方式为我的Python代码创建gRPC绑定:

python -m grpc_tools.protoc -I $(pwd)/protos --python_out=./fino/pb2 --grpc_python_out=./fino/pb2 -I=$GOPATH/src -I=$GOPATH/src/github.com/gogo/protobuf/protobuf $(pwd)/protos/*
但是生成的文件有一个不存在的依赖项:
from github.com.gogo.protobuf.gogoproto import gogo_pb2 as github_dot_com_dot_gogo_dot_protobuf_dot_gogoproto_dot_gogo__pb2

这将在之后用于:

DESCRIPTOR = _descriptor.FileDescriptor(
  name='oracle.proto',
  package='oracle',
  syntax='proto2',
  serialized_pb=_b('\n\x0coracle.proto\x12\x06oracle\x1a-github.com/gogo/protobuf/gogoproto/gogo.proto\x1a\x1fgoogle/protobuf/timestamp.proto\x1a\x1bgoogle/protobuf/empty.proto\x1a\x0btypes.proto\":\n\x0b\x41\x63\x63ountList\x12+\n\x08\x61\x63\x63ounts\x18\x01 \x03(\x0b...')
  ,
  dependencies=[github_dot_com_dot_gogo_dot_protobuf_dot_gogoproto_dot_gogo__pb2.DESCRIPTOR,google_dot_protobuf_dot_timestamp__pb2.DESCRIPTOR,google_dot_protobuf_dot_empty__pb2.DESCRIPTOR,types__pb2.DESCRIPTOR,])
显然,我不能运行这份代码。在尝试删除不存在的导入之后:
TypeError: Couldn't build proto file into descriptor pool!
Invalid proto descriptor for file "oracle.proto":
  oracle.proto: Import "github.com/gogo/protobuf/gogoproto/gogo.proto" has not been loaded.

我尝试添加了

--include_imports --descriptor_set_out=$(pwd)/protos/all.proto  

但我不确定如何将其添加到我的Python文件中。我只想在我的Python代码库中拥有自包含的描述。

编辑1: 示例proto文件:

syntax = "proto2";
package etcdserverpb;

import "github.com/gogo/protobuf/gogoproto/gogo.proto";

option (gogoproto.marshaler_all) = true;
option (gogoproto.sizer_all) = true;
option (gogoproto.unmarshaler_all) = true;
option (gogoproto.goproto_getters_all) = false;

message Request {
    optional uint64 ID         =  1 [(gogoproto.nullable) = false];
    optional string Method     =  2 [(gogoproto.nullable) = false];
    optional string Path       =  3 [(gogoproto.nullable) = false];
    optional string Val        =  4 [(gogoproto.nullable) = false];
    optional bool   Dir        =  5 [(gogoproto.nullable) = false];
    optional string PrevValue  =  6 [(gogoproto.nullable) = false];
    optional uint64 PrevIndex  =  7 [(gogoproto.nullable) = false];
    optional bool   PrevExist  =  8 [(gogoproto.nullable) = true];
    optional int64  Expiration =  9 [(gogoproto.nullable) = false];
    optional bool   Wait       = 10 [(gogoproto.nullable) = false];
    optional uint64 Since      = 11 [(gogoproto.nullable) = false];
    optional bool   Recursive  = 12 [(gogoproto.nullable) = false];
    optional bool   Sorted     = 13 [(gogoproto.nullable) = false];
    optional bool   Quorum     = 14 [(gogoproto.nullable) = false];
    optional int64  Time       = 15 [(gogoproto.nullable) = false];
    optional bool   Stream     = 16 [(gogoproto.nullable) = false];
    optional bool   Refresh    = 17 [(gogoproto.nullable) = true];
}

message Metadata {
    optional uint64 NodeID    = 1 [(gogoproto.nullable) = false];
    optional uint64 ClusterID = 2 [(gogoproto.nullable) = false];
}

这里是从 https://github.com/gogo/protobuf/issues/376 继续的内容。


我觉得你的命令行中同时有-I<空格><参数>-I=<参数>看起来很奇怪。而且,我不清楚-I=$GOPATH/src -I=$GOPATH/src/github.com/gogo/protobuf/protobuf的目的是什么。这些是怎么出现在里面的?你想让它们做什么?你是根据哪个示例进行工作的?此外,你能分享一下你的.proto内容吗? - Nathaniel Manista At Google
我正在使用gogoproto生成Golang代码,其中使用了一些很酷的扩展,来控制生成的Go代码的外观。但是这会导致Python程序和创建Python结构方面出现问题。我更喜欢使用现代的proto3,但最重要的是我需要可工作的代码。 - nmiculinic
1个回答

阿里云服务器只需要99元/年,新老用户同享,点击查看详情
2
我知道这是一个老问题,但我想尝试解决它。我的解决方法如下:
  1. 下载github.com/gogo/protobuf/gogoproto/gogo.proto文件并将其命名为gogo.proto,存放在$(pwd)/protos文件夹中。
  2. import "github.com/gogo/protobuf/gogoproto/gogo.proto";替换为import "gogo.proto";
  3. 更改命令以显式地使用proto文件:python -m grpc_tools.protoc -I $(pwd)/protos --python_out=./fino/pb2 --grpc_python_out=./fino/pb2 -I=$GOPATH/src -I=$GOPATH/src/github.com/gogo/protobuf/protobuf $(pwd)/protos/gogo.proto $(pwd)/protos/metadata.proto(假设您将示例proto文件命名为metadata.proto)。
"Original Answer"翻译成"最初的回答"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,