I am trying to use Google Protocol Buffers in a Python 3 project. However the generated python files do not want to cooperate with the google.protobuf library. Trying to use the protobuf objects results in a NotImplementedError.
My setup:
The problem appears when using these libraries:
Example:
from pb_test import test_pb2
pb_object = test_pb2.TestMsg()
pb_object.Clear() # results in NotImplementedError
The fact that the same problem occurs when using two different libraries is a strong hint towards having an invalid test_pb2.py file. The 'unimplemented' methods are located in Message class, which is supposed to be overridden by metaclass. It seems that the metaclass is not applied at all.
The test.proto file:
message TestMsg {
required int32 id = 1;
}
The file is compiled using this command:
eipifi@debvm:~/pb_test$ protoc --python_out=. test.proto
Any hints would be appreciated.
Solved. In order to make the *_pb2.py files play well with the protobuf libraries in Python 3, the files need to be changed in a following way:
Original:
class TestMsg(_message.Message):
__metaclass__ = _reflection.GeneratedProtocolMessageType
DESCRIPTOR = _TESTMSG
Fixed:
class TestMsg(_message.Message, metaclass=_reflection.GeneratedProtocolMessageType):
DESCRIPTOR = _TESTMSG
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