Protobuf in Python

Posted on Tue 13 March 2018 in articles

Protobuf is a method to serializing structured objects. It helps to develop applications which are communicating with each other over a wire. protoc is the code generator by reading the .proto files and it provides Python support.

First, we will need to install the Protocol Buffers compiler protoc. In macOS, it is easy to install using brew:

$ brew install protobuf

Then we can use protoc command to generate language bindings, e.g., Python:

$ protoc --python_out=/path/to/your/dest/directory your.proto

And install protobuf runtime in Python using pip:

$ pip install protobuf

Make sure the runtime version is the same as protoc binary, or it may not work.