我有两个这样的包
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
然后像下面这样在目录中编译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.
如何解决这个导入问题?
最佳答案
有人在以下问题中回答了我的问题:https://github.com/google/protobuf/issues/4176,in简短地指出,includes的用法不一致是根,解决方案是
cd /pathToSrcDir/
protoc -I. --python_out=. com/abc/protobuf/share.proto
protoc -I. --python_out=. com/abc/depart/detect.proto