3

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 use 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 from this page : https://developers.google.com/protocol-buffers/docs/pythontutorial

Would you have an idea why the metaclass is not generated ?

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

Tekuu59
  • 33
  • 4

1 Answers1

5

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

jsofri
  • 227
  • 1
  • 10
  • 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. – Tekuu59 Jun 02 '22 at 10:09
  • Another option is to just add --pyi_out arg - it will generate _bp2.pyi file with classes – Krzysiek Nov 25 '22 at 16:51