Ubuntu – Installing Google Protobuf on Ubuntu 15.10

15.10python-2.7

After downloading, building and installing Google Protobuf, I cannot import it in Python

import google.protobuf

cannot find the module. Typing

protoc --version

returns 3.

Any suggestion on how to get this fixed ? I would not like to use "pip install protobuf" as it installs version 2.6, and I need 3.

Best Answer

The protobuf library and protoc are 2 different things completely.

protoc (called "protobuf-compiler" by apt-get) is an executable that takes .proto files and generates code in the chosen language.

Meanwhile, a protobuf library, like most libraries, contains code for you to reference in your own code - or in this case, code that is referenced by the generated code that protoc outputs.

The protobuf library for python (called "python-protobuf" by apt-get) can be easily updated to v3+ using this command:

sudo pip install --upgrade protobuf

I know of no better way to check the version of your protobuf library for python than this:

python -c "import google.protobuf; print google.protobuf.__version__"

protoc, on the other hand, is significantly harder to update to v3+. Luckily, you basically only need to update it if you want to use v3 syntax in your .proto definitions.

If you decide to, you should only ever update protoc by downloading source code or binaries from https://github.com/google/protobuf as described in their README.

Related Question