在 Ubuntu 15.10 上安装 Google Protobuf

在 Ubuntu 15.10 上安装 Google Protobuf

下载、构建和安装 Google Protobuf 后,我无法将其导入 Python

import google.protobuf

找不到模块。输入

protoc --version

返回 3。

关于如何修复此问题,有什么建议吗?我不想使用“pip install protobuf”,因为它会安装 2.6 版本,而我需要 3 版本。

答案1

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

protoc(称为“原始缓冲Capt-get 提供的“ompiler”是一个可执行文件,它可以获取 .proto 文件并以所选语言生成代码。

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

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

sudo pip install --upgrade protobuf

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

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

另一方面,protoc 更新到 v3+ 则困难得多。幸运的是,基本上只有在您想在 .proto 定义中使用 v3 语法时才需要更新它。

如果你决定这样做,你应该只通过从以下位置下载源代码或二进制文件来更新 protochttps://github.com/google/protobuf如其 README 中所述。

相关内容