在 Ubuntu 15.10 上安装 Google Protobuf

1 python-2.7 15.10

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

import google.protobuf
Run Code Online (Sandbox Code Playgroud)

找不到模块。打字

protoc --version
Run Code Online (Sandbox Code Playgroud)

返回 3。

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

M. *_*ler 7

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

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

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

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

sudo pip install --upgrade protobuf
Run Code Online (Sandbox Code Playgroud)

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

python -c "import google.protobuf; print google.protobuf.__version__"
Run Code Online (Sandbox Code Playgroud)

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

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