I'm using libprotoc 3.2.0 with Python 3 in a Python package. If I try to the Protobuff Python files (denoted with the extensions _pb2.py) in to their own folder, with the .proto files, let's say protobufs and then try to import them a python file as follows:
# assuming outputs from a.proto and b.proto, where b depends on a
import protobufs.a
import protobufs.b
I get import errors that b can't find a. If I output the _pb2.py files into the root of my package, I don't have this problem. This is elaborated upon in this issue, but I'm not sure if I'm having the exact same issue or not. Is is possible to avoid outputting the _pb2.py files at the root of my package?
What I actually have is two protobuffs that reference each-other, as follows in the .proto file:
syntax = "proto3";
import "common.proto";
In the Python file, this is translated to:
# Generated by the protocol buffer compiler. DO NOT EDIT!
# source: persons.proto
import sys
_b=sys.version_info[0]<3 and (lambda x:x) or (lambda x:x.encode('latin1'))
from google.protobuf.internal import enum_type_wrapper
from google.protobuf import descriptor as _descriptor
from google.protobuf import message as _message
from google.protobuf import reflection as _reflection
from google.protobuf import symbol_database as _symbol_database
from google.protobuf import descriptor_pb2
# @@protoc_insertion_point(imports)
_sym_db = _symbol_database.Default()
import common_pb2 as common__pb2
But because the file is in a folder called protobufs in a Python package, the last line should be import protobufs.common_pb2 as common__pb2 and not import common_pb2 as common__pb2, but libprotoc doesn't know how to take the folder into account.
The solution is actually quite straightforward. Simply use:
import "protobufs/common.proto";
This should cause your _pb2.py file to contain the import from protobufs import common_pb2 as protobufs_dot_common__pb2 instead of the current import.
Of course, you'll need to update your compiler's command line accordingly. Something in the line of:
protoc.exe protobufs\persons.proto --proto_path=. --python_out=.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With