Alex Luya
Alex Luya

Reputation: 9912

How to import protobuff message in different package's .proto file correctly?

I have two packages like this

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

and the conent of share.proto like this:

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

and the content of detect.proto like this:

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

and compile share.proto in it's dir like this:

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

then compile detect.proto in it's dir like this:

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

and

pathToSrcDir has been added to PYTHONPATH

all compilations work fine,but when run a python script which

from com.abc.depart import detect_pb2

got this error

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.

How to solve this import problem?

Upvotes: 6

Views: 10130

Answers (1)

Alex Luya
Alex Luya

Reputation: 9912

Some guy answered my question and in short, an inconsistent use of includes is the root of the problem.

The solution is

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

Upvotes: 7

Related Questions