首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >protobuf生成一个不存在的依赖项

protobuf生成一个不存在的依赖项
EN

Stack Overflow用户
提问于 2018-02-09 14:55:19
回答 1查看 2.9K关注 0票数 14

我试图通过以下方法为我的python代码创建一个gRPC绑定:

代码语言:javascript
运行
复制
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/*

但是生成的文件有一个不存在的依赖项:

代码语言:javascript
运行
复制
from github.com.gogo.protobuf.gogoproto import gogo_pb2 as github_dot_com_dot_gogo_dot_protobuf_dot_gogoproto_dot_gogo__pb2

后来被用于:

代码语言:javascript
运行
复制
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,])

显然,我不能运行这段代码。尝试删除不存在的导入后:

代码语言:javascript
运行
复制
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.

我试着加入

代码语言:javascript
运行
复制
--include_imports --descriptor_set_out=$(pwd)/protos/all.proto  

但我不知道如何将它添加到我的python文件中。我想要的只是我的python代码库中的自我包含的描述。

EDIT1: proto文件示例:

代码语言:javascript
运行
复制
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的延续

EN

回答 1

Stack Overflow用户

发布于 2019-07-01 15:51:51

我知道这是个老问题,但我想我该试一试。我为解决这个问题所做的:

  1. github.com/gogo/protobuf/gogoproto/gogo.proto文件下载到您的$(pwd)/protos文件夹;将该文件命名为gogo.proto
  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)。
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48708496

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档