How to Install Google Protocol Buffers on Raspberry Pi and Test with Python

How to Install Google Protocol Buffers on Raspberry Pi and Test with Python.md

How to Install Google Protocol Buffers on Raspberry Pi and Test with Python

1. Installation

Make sure you have Python 2.6 or newer but not Python 3.x yet. If in doubt, then run:

$ python -V

1.1 Install C++ Protocol Compiler

We need C++ Protocol Compiler for python. This is required to compile .proto file

$ cd ~/
$ sudo apt-get install autoconf automake libtool curl python-dev
$ wget https://github.com/google/protobuf/releases/download/v2.6.1/protobuf-2.6.1.tar.gz
$ tar -zxvf protobuf-2.6.1.tar.gz
$ cd protobuf-2.6.1/
$ ./configure
$ make
$ make check
$ sudo make install
$ sudo ldconfig

Note: You can also refer to the full installation guide from here OR read README.md from pytobuf-2.6.1 folder

1.2 Build Python runtime library

This is required to run Python program. We will use c++ implementation for Python runtime.

$ cd ~/
$ cd protobuf-2.6.1/
$ cd python
$ export LD_LIBRARY_PATH=../src/.libs
$ python setup.py build --cpp_implementation
$ python setup.py test --cpp_implementation
$ sudo python setup.py install --cpp_implementation
$ export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=cpp
$ export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION_VERSION=2

Note: I cannot run $ python setup.py test --cpp_implementation for some reason. but you should try as it is stated in the full installation guide.

Note: You can refer to full installation guide from here OR read README.txt file in the python folder

2. Testing Protocol Message

2.1 Write .proto file

We need to write a protocol message. This can be use for both server side and client side if you are going to transfer across the network.

$ cd /~
$ mkdir ProtobuffTest
$ cd ProtobuffTest
$ gedit addressbook.proto

Copy the following text and save it as addressbook.proto

package message;

message Person {
    required string name = 1;
    optional string email = 2;
}
message AddressBook{
    repeated Person person = 1;
}

2.2 Compile Protocol Message

Before we use any of the protocol message, we first need to compile once. In the current folder, type

$ protoc addressbook.proto --python_out="."

After the compilation, addressbook_pb2.py will be generated and we will use this file for Python program.

2.3 Compile Python Program

We first create a python program first.

$ gedit AddressBookTest.py

Copy the following text and save it as AddressBookTest.py

#! /usr/bin/python

import addressbook_pb2

address_book = addressbook_pb2.AddressBook()

john = address_book.person.add()
john.name = "John"
john.email = "john@abc.com"

drake = address_book.person.add()
drake.name = "Drake"
drake.email = "drake@abc.com"

for person in address_book.person:
    print " Hello ", person.name
    print " Your email will be ", person.email

Just like any other Python program, you can just run the program by typing

$ python AddressBookTest.py

The output will be like this.

 Hello  John
 Your email will be  john@abc.com
 Hello  Drake
 Your email will be  drake@abc.com

Reference

  1. SmallTalk Protocol Buffers
  2. Protocol Buffers - Google’s data interchange format
  3. Protocol Buffer Basics: Python

4 comments:

  1. Hi there - got the following when trying to build the runtime library - any help/pointers appreciated

    pi@raspberrypi:~/protobuf-2.6.1/python $ python setup.py build --cpp_implementation
    running build
    running build_py
    running build_ext
    building 'google.protobuf.pyext._message' extension
    arm-linux-gnueabihf-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fno-strict-aliasing -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-security -fPIC -DGOOGLE_PROTOBUF_HAS_ONEOF=1 -I. -I../src -I/usr/include/python2.7 -c google/protobuf/pyext/descriptor.cc -o build/temp.linux-armv6l-2.7/google/protobuf/pyext/descriptor.o
    cc1plus: warning: command line option ‘-Wstrict-prototypes’ is valid for C/ObjC but not for C++
    google/protobuf/pyext/descriptor.cc:33:20: fatal error: Python.h: No such file or directory
    #include
    ^
    compilation terminated.
    error: command 'arm-linux-gnueabihf-gcc' failed with exit status 1

    ReplyDelete
    Replies
    1. Hi! Could you solve it? I'm getting the same error message

      Delete
  2. Hi there - got the following when trying to test

    from google.protobuf.pyext import _message
    ImportError: libprotobuf.so.19: cannot open shared object file: No such file or directory
    Ran 41 tests in 0.060s



    FAILED (errors=39)
    Test failed:
    error: Test failed:

    ReplyDelete
  3. Is `make check` a compulsory step? Doesn't it only checks if all files and configurations are inplace and okay? If yes, then it would be a waste of time to use this command taking almost 2 hours.

    ReplyDelete