如何正确导入不同包的.proto文件中的protobuff消息?

时间:2018-01-17 03:05:11

标签: python protocol-buffers

我有两个像这样的包

com.abc.
         protobuf.
                    share.proto
         depart.
                    detect.proto 

和share.proto的坎坷如下:

syntax = "proto3";
package com.adc.protobuf;
message Test{}

和detect.proto的内容如下:

syntax = "proto3";
package com.adc.depart;
import "com/abc/protobuf/share.proto"

并在其中编译share.proto,如下所示:

protoc -I=. --python_out=. share.proto

然后在它的dir中编译detect.proto:

protoc -I=/pathToSrcDir/ -I=. --python_out=. detect.proto 

pathToSrcDir has been added to PYTHONPATH

所有编译工作正常,但在运行python脚本时

from com.abc.depart import detect_pb2

收到此错误

TypeError: Couldn't build proto file into descriptor pool!
Invalid proto descriptor for file "detect.proto":
  detect.proto: Import "com/abc/protobuf/share.proto" has not been loaded.
  com.abc.depert.XClass.ymethod: "com.abc.protobuf.Test" seems to be defined in "share.proto", which is not imported by "detect.proto".  To use it here, please add the necessary import.

如何解决此导入问题?

1 个答案:

答案 0 :(得分:0)

有人在我的问题中回答了我的问题:https://github.com/google/protobuf/issues/4176,in简短来说,包含的不一致使用是根,解决方案是

cd /pathToSrcDir/
protoc -I. --python_out=. com/abc/protobuf/share.proto 
protoc -I. --python_out=. com/abc/depart/detect.proto