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

2024-10-01 07:22:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个这样的包裹

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

以及共享.proto像这样:

^{pr2}$

以及检测.proto像这样:

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

编译共享.proto它的方向是这样的:

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

然后编译检测.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.

如何解决这个进口问题?在


Tags: toimportcomshareout方向protohas
1条回答
网友
1楼 · 发布于 2024-10-01 07:22:02

有人回答了我的问题: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

相关问题 更多 >