Skip to content

Python protobuf

Install

1
2
3
sudo apt install protobuf-compiler

pip install protobuf

Simple demo

proto/person.proto
1
2
3
4
5
6
7
syntax = "proto3";

message Person {
  string name = 1;
  int32 id = 2;
  string email = 3;
}
  • Import and create person class
  • Serialize
  • Deserialization ParseFromString
from msgs import person_pb2

# Create a Person object
person = person_pb2.Person()
person.name = "Alice"
person.id = 123
person.email = "alice@example.com"

# Serialize to bytes
data = person.SerializeToString()

# Deserialize
new_person = person_pb2.Person()
new_person.ParseFromString(data)

print(f"Name: {new_person.name}, ID: {new_person.id}, Email: {new_person.email}")

Python intellisense

pip install mypy-protobuf
  • Create pyi file for
    1
    2
    3
    4
    protoc \
    --python_out=msgs \
    --mypy_out=msgs \
    --proto_path=proto person.proto