I need to read a protobuf message in Python. I’ve defined the message in a file order.proto
:
syntax = "proto3";
message Order {
string orderId = 1;
string userId = 2;
repeated string products = 3;
string createdAt = 4;
}
I generated the corresponding Python class order_pb2.py
.
# -*- coding: utf-8 -*-
# Generated by the protocol buffer compiler. DO NOT EDIT!
# source: order.proto
"""Generated protocol buffer code."""
from google.protobuf import descriptor as _descriptor
from google.protobuf import message as _message
from google.protobuf import reflection as _reflection
from google.protobuf import symbol_database as _symbol_database
# @@protoc_insertion_point(imports)
_sym_db = _symbol_database.Default()
DESCRIPTOR = _descriptor.FileDescriptor(
name='order.proto',
package='',
syntax='proto3',
serialized_options=None,
create_key=_descriptor._internal_create_key,
serialized_pb=b'nx0border.proto"Mnx05Orderx12x0fnx07orderIdx18x01 x01(tx12x0enx06userIdx18x02 x01(tx12x10nx08productsx18x03 x03(tx12x11ntcreatedAtx18x04 x01(tbx06proto3'
)
_ORDER = _descriptor.Descriptor(
name='Order',
full_name='Order',
filename=None,
file=DESCRIPTOR,
containing_type=None,
create_key=_descriptor._internal_create_key,
fields=[
_descriptor.FieldDescriptor(
name='orderId', full_name='Order.orderId', index=0,
number=1, type=9, cpp_type=9, label=1,
has_default_value=False, default_value=b"".decode('utf-8'),
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
serialized_options=None, file=DESCRIPTOR, create_key=_descriptor._internal_create_key),
_descriptor.FieldDescriptor(
name='userId', full_name='Order.userId', index=1,
number=2, type=9, cpp_type=9, label=1,
has_default_value=False, default_value=b"".decode('utf-8'),
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
serialized_options=None, file=DESCRIPTOR, create_key=_descriptor._internal_create_key),
_descriptor.FieldDescriptor(
name='products', full_name='Order.products', index=2,
number=3, type=9, cpp_type=9, label=3,
has_default_value=False, default_value=[],
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
serialized_options=None, file=DESCRIPTOR, create_key=_descriptor._internal_create_key),
_descriptor.FieldDescriptor(
name='createdAt', full_name='Order.createdAt', index=3,
number=4, type=9, cpp_type=9, label=1,
has_default_value=False, default_value=b"".decode('utf-8'),
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
serialized_options=None, file=DESCRIPTOR, create_key=_descriptor._internal_create_key),
],
extensions=[
],
nested_types=[],
enum_types=[
],
serialized_options=None,
is_extendable=False,
syntax='proto3',
extension_ranges=[],
oneofs=[
],
serialized_start=15,
serialized_end=92,
)
DESCRIPTOR.message_types_by_name['Order'] = _ORDER
_sym_db.RegisterFileDescriptor(DESCRIPTOR)
Order = _reflection.GeneratedProtocolMessageType('Order', (_message.Message,), {
'DESCRIPTOR' : _ORDER,
'__module__' : 'order_pb2'
# @@protoc_insertion_point(class_scope:Order)
})
_sym_db.RegisterMessage(Order)
# @@protoc_insertion_point(module_scope)
I import it using the following:”
from order_pb2 import Order
@app.route('/', methods=['POST'])
def onNewOrder():
order = Order()
Even though the type of order
is Order
, I’m unable to access functions like ParseFromString
or fields such as orderId
. My IDE indicates that the attributes ParseFromString
and orderId
are unknown.
Additionally, there’s a bit of an issue: in order_pb2.py
, the line at the very bottom, _sym_db.RegisterMessage(Order)
, shows an error. It indicates that the argument Order
of type type[Order]
cannot be assigned to the parameter message
of type type[Message]
.
Installed:
protoc –version: libprotoc 25.1
pip protobuf: 5.28.0
Python: 3.12.5
IDE: Visual Studio Code
I work in a python virtual env project. The Interpreter is set to python 3.12.5 (‘env’: venv’)
what am I doing wrong? I hope for help.
3
I tried in VSCode with the below versions
PS C:UsersXDesktopNew folder> protoc --version
libprotoc 28.0
PS C:UsersXDesktopNew folder>
PS C:UsersXDesktopNew folder> pip show protobuf
Name: protobuf
Version: 5.28.0
And below code, it seems to be working
from test_pb2 import Order
order = Order()
order.orderId = '1'
print(order.orderId) # this will show '1'
print(order.products) # this will show empty list
Even though the type of order is Order, I’m unable to access functions like ParseFromString or fields such as orderId. My IDE indicates that the attributes ParseFromString and orderId are unknown.
I am assuming you are saying that when you try to access fields using the dot operator, It doesn’t autocomplete or something, you can try and use print(hasattr(order, 'orderId'))
to check if your order has that attribute or not.