Why python code generation from proto is not generating classes?
Asked Answered
W

1

6

I'm currently trying to generate python code from a proto file.

My proto file looks like this:

syntax = "proto3";

package display;

message Hello {
  uint32 version = 1;
  uint32 value = 2;
  int32 id = 3;
}

I used this protoc command to generate the python code:

protoc -I="." --python_out="." test.proto

And here is the resulting python file:

# -*- coding: utf-8 -*-
# Generated by the protocol buffer compiler.  DO NOT EDIT!
# source: test.proto
"""Generated protocol buffer code."""
from google.protobuf.internal import builder as _builder
from google.protobuf import descriptor as _descriptor
from google.protobuf import descriptor_pool as _descriptor_pool
from google.protobuf import symbol_database as _symbol_database
# @@protoc_insertion_point(imports)

_sym_db = _symbol_database.Default()




DESCRIPTOR = _descriptor_pool.Default().AddSerializedFile(b'\n\ntest.proto\x12\x07\x64isplay\"3\n\x05Hello\x12\x0f\n\x07version\x18\x01 \x01(\r\x12\r\n\x05value\x18\x02 \x01(\r\x12\n\n\x02id\x18\x03 \x01(\x05\x62\x06proto3')

_builder.BuildMessageAndEnumDescriptors(DESCRIPTOR, globals())
_builder.BuildTopDescriptorsAndMessages(DESCRIPTOR, 'test_pb2', globals())
if _descriptor._USE_C_DESCRIPTORS == False:

  DESCRIPTOR._options = None
  _HELLO._serialized_start=23
  _HELLO._serialized_end=74
# @@protoc_insertion_point(module_scope)

It doesn't look at all like the documentation from Google on this page.

Why isn't the metaclass generated?

I'm using Python 3.9 with the latest version of the protobuf package and last version of protoc.

Wilfredwilfreda answered 2/6, 2022 at 9:1 Comment(0)
G
5

add --grpc_python_out="." to the protoc command. this will generate an additional script with the required classes

Gradate answered 2/6, 2022 at 9:8 Comment(2)
Thanks a lot, it worked for me. I'd add, if somebody else encounter this error, install first grpc_tools with pip install grpc_tools.Wilfredwilfreda
Another option is to just add --pyi_out arg - it will generate <name>_bp2.pyi file with classesMcclendon

© 2022 - 2024 — McMap. All rights reserved.