【问题标题】:Having problems using the python code generated from the google protocol buffer使用从 google 协议缓冲区生成的 python 代码时遇到问题
【发布时间】:2015-01-21 15:36:00
【问题描述】:

当我尝试运行 gtfs_realtime_pb2.py(通过谷歌的协议缓冲区运行 gtfs-realtime.proto 生成的 python 代码)时出现以下错误:

Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    from google.protobuf import descriptor
ImportError: No module named 'google'

这是我遇到问题的特定代码的链接: https://github.com/mattwigway/gtfsrdb

以及安装google协议缓冲区的链接:
https://developers.google.com/protocol-buffers/

我很确定我已经正确安装了缓冲区,为什么它会抛出找不到这个模块?

【问题讨论】:

  • 我对python PB不熟悉。但是对于 Java PB,有一个单独的 java 安装步骤,其中 java 类是从 descriptor.proto 生成的。听起来您的安装中缺少描述符类。
  • 我对协议缓冲区工作原理的理解是,你给了它一个 .proto 文件,其中列出了消息的结构,然后缓冲区生成一个代码文件,其中包含你的所有消息类选择的语言。
  • 如果你将 google 提供的 gtfs-realtime.proto 文件运行到协议缓冲区中,它会生成包含类的 gtfs-realtime.py。您是说在此过程中还会生成其他东西,python 会调用它来解释实时提要?

标签: python protocol-buffers importerror gtfs


【解决方案1】:

协议缓冲区中包含descriptor.proto;此原型的 python 代码应作为安装过程的一部分生成。

您正在使用的协议缓冲区使用descriptor.proto,因此需要通过--proto_path= 参数包含它所在的目录(请参阅Importing "google/protobuf/descriptor.proto" in java protocol buffers

对于窗口系统,安装目录将是您放置它的位置;对于*nix,它可能是answer taken from

/usr/include/descriptor.proto 或者 /usr/local/include/descriptor.proto

即对于java,你会做类似的事情

protoc addressbookSD.proto --java_out=./ --proto_path=./ --proto_path=<protobuf-install-directory>/src

其中 是协议缓冲区安装目录。关键是descriptor.proto在

<protobuf-install-directory>\src\google\protobuf

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-25
    • 1970-01-01
    • 1970-01-01
    • 2014-01-16
    相关资源
    最近更新 更多