【问题标题】:Protocol Buffers in Python 3 - NotImplementedErrorPython 3 中的协议缓冲区 - NotImplementedError
【发布时间】:2014-07-30 12:10:18
【问题描述】:

我正在尝试在 Python 3 项目中使用 Google 协议缓冲区。但是生成的 python 文件不想与 google.protobuf 库合作。尝试使用 protobuf 对象会导致 NotImplementedError。

我的设置:

  • Python 3.4.1
  • 协议 2.5.0

使用这些库时出现问题:

例子:

from pb_test import test_pb2
pb_object = test_pb2.TestMsg()
pb_object.Clear()  # results in NotImplementedError

使用两个不同的库时会出现同样的问题,这强烈暗示了 test_pb2.py 文件无效。 “未实现”的方法位于 Message 类中,它应该被元类覆盖。似乎根本没有应用元类。

test.proto 文件:

message TestMsg {
  required int32 id = 1;
}

使用以下命令编译文件:

eipifi@debvm:~/pb_test$ protoc --python_out=. test.proto

任何提示将不胜感激。

【问题讨论】:

    标签: python python-3.x protocol-buffers


    【解决方案1】:

    解决了。为了使 *_pb2.py 文件与 Python 3 中的 protobuf 库很好地兼容,需要按以下方式更改文件:

    原文:

    class TestMsg(_message.Message):
    __metaclass__ = _reflection.GeneratedProtocolMessageType
    DESCRIPTOR = _TESTMSG
    

    固定:

    class TestMsg(_message.Message, metaclass=_reflection.GeneratedProtocolMessageType):
    DESCRIPTOR = _TESTMSG
    

    【讨论】:

      猜你喜欢
      • 2017-03-27
      • 1970-01-01
      • 2011-09-18
      • 2021-12-19
      • 2011-11-15
      • 1970-01-01
      • 1970-01-01
      • 2020-04-06
      • 2016-12-19
      相关资源
      最近更新 更多