简体   繁体   中英

Protobuf, import error after compilation in python3.6

Directory structure:

Application/
   proto/
     payload.proto
     lab_account.proto
     public_trail.proto
   protocompiled/
     payload_pb2.py
     lab_account_pb2.py

Contents of the payload.proto

syntax = "proto3";
import "lab_account.proto";
import "public_trail.proto";

if I compile my payload.proto file, with command .

 Application⟫ protoc --proto_path=./proto --python_out=./protocompiled payload.proto

The compiled payload_pb2.py doest have the required imports. It has wrong import statement like this.

 import lab_account_pb2 as lab__account__pb2
 import public_trail_pb2 as public__trail__pb2

instead of this;

 import protocompiled.lab_account_pb2 as lab__account__pb2
 import protocompiled.public_trail_pb2 as public__trail__pb2

Also referred, https://github.com/protocolbuffers/protobuf/issues/1491 but couldnt solve the issue.

There is an open issue 5374 in protobuf github regarding this problem.

Until it is resolved, I use the following workaround: after protoc run a sed script (works at least for GNU sed ) which will add relative imports.

protoc $PATH/*.proto --python_out=$PROTOC_OUTDIR
sed -i $PROTOC_OUTDIR/*_pb2.py -e 's/^import [^ ]*_pb2/from . \0/'

But this works only when all files are in the same directory. More complex script will be required to handle files spread over multiple directories.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM