在Ubuntu 15.10上安装Google Protobuf

下载,构建和安装Google Protobuf后,我无法在Python中导入它

import google.protobuf 

找不到模块。 打字

 protoc --version 

返回3。

关于如何解决此问题的任何建议? 我不想使用“pip install protobuf”,因为它安装2.6版本,我需要3。

protobuf库和protoc完全是两个不同的东西。

protoc(由apt-get称为“ proto buf-o ompiler”)是一个可执行文件,它接受.proto文件并以所选语言生成代码。

同时,与大多数库一样,protobuf库包含您可以在自己的代码中引用的代码 – 或者在这种情况下,代码由protoc输出的生成代码引用。

python的protobuf库(apt-get称为“python-protobuf”)可以使用以下命令轻松更新到v3 +:

 sudo pip install --upgrade protobuf 

我知道没有更好的方法来检查你的protobuf库的python版本比这个:

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

另一方面,protoc更难以更新到v3 +。 幸运的是,如果要在.proto定义中使用v3语法,基本上只需要更新它。

如果您决定,您应该只通过从https://github.com/google/protobuf下载源代码或二进制文件来更新protoc,如其自述文件中所述。