Why do I see “cannot import name descriptor_pb2” error when using Google Protocol Buffers?

前端 未结 5 628
隐瞒了意图╮
隐瞒了意图╮ 2021-01-18 03:51

When using the generated Python code from our protobuf classes, we get this error:

cannot import name descriptor_pb2

The equivalent C++ gen

5条回答
  •  旧巷少年郎
    2021-01-18 04:13

    I believe you have to generate descriptor_pb2.py with protoc yourself:

    protoc descriptor.proto --python_out=gen/
    

    gen/ is a folder with generated python classes.

    After that, the following works just fine:

    sys.path.append('../gen')
    from descriptor_pb2 import FileDescriptorSet
    

    ../gen/descriptor_pb2.py must exists.

提交回复
热议问题