【问题标题】:Read Protocol Buffers file using Python使用 Python 读取 Protocol Buffers 文件
【发布时间】:2016-10-25 14:28:54
【问题描述】:

我的目标是解析扩展名为 filename.pb.string 的 Protocol Buffers 文件。在 Mac 上使用 Homebrew 下载 Protobuff。 Ran protoc --version 并拥有 libprotoc 3.1.0 版本。

但是当我运行 Python 时,它说找不到模块。我将 .pb 文件名更改为 _pb2.py 并在我的 Python 脚本中导入模块。

import filename_pb2 as proto

我正在使用 Google 文档,但仍然没有任何运气。我在通过 Python 编译 Protobuf .so 文件时也遇到了问题。我只是无法处理 Protobuf 和 Python 如何链接在一起。有人可以指导我吗?

Python 错误

import response_123_pb2 as proto
ImportError: No module named response_123_pb2

【问题讨论】:

  • “找不到模块”?请发布完整的错误。

标签: python homebrew protocol-buffers


【解决方案1】:

你必须将你的 .proto 文件编译成 *_pb2.py 文件:

protoc --python_out=./YourProto.proto

确保它生成一个名为 YourProto_pb2.py 的文件。然后你可以将它导入到你的代码中:

import YourProto_pb2 as proto

如果您在编译时遇到其他问题,那就另当别论了。请务必查看此documentation

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-15
    • 1970-01-01
    • 2018-12-20
    • 2012-02-27
    • 2012-07-11
    • 1970-01-01
    相关资源
    最近更新 更多