Idiomatic Protocol Buffers for Python.
Actively maintained. Last release 6 weeks ago, commits in 23 of the last 26 weeks. A safe choice.
pip install protobuf-pyprotobuf-py
protobuf-py is an ergonomic and modern Protobuf library for Python.
Well-typed, generated code you can read. High-performance encoder/decoder written in Rust. 100% conformance.
Zero third-party dependencies or tooling required.
from gen.user_pb import User
# Messages are plain Python objects: pass fields as keyword args, or set them later.
user = User(id="123", first_name="Alice")
user.last_name = "Smith"
# Serialize to the Protobuf wire format, then parse it back.
data = user.to_binary()
user = User.from_binary(data)
print(user.first_name) # Alice
print(user.to_json()) # {"id": "123", "firstName": "Alice", "lastName": "Smith"}
Protobuf is the easiest way to build APIs. We recommend using it with Connect for Python, which generates server stubs for you:
from connectrpc.request import RequestContext
from gen.user_connect import UserService, UserServiceASGIApplication
from gen.user_pb import GetUserRequest, GetUserResponse, User
class UserHandler(UserService):
async def get_user(
self, request: GetUserRequest, ctx: RequestContext
) -> GetUserResponse:
user = User(id=request.id, first_name="Alice", last_name="Smith")
return GetUserResponse(user=user)
# Serve it with any ASGI server, e.g. `uvicorn server:app`.
app = UserServiceASGIApplication(UserHandler())
Connect can also generate client libraries for every major language, including your frontend. Here's what the generated Python client looks like: