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.

1 Answer 1

5

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

Sign up to request clarification or add additional context in comments.

2 Comments

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.
Another option is to just add --pyi_out arg - it will generate <name>_bp2.pyi file with classes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.