【问题标题】:How to use proto files in googleapis-common-protos in Python?如何在 Python 的 googleapis-common-protos 中使用 proto 文件?
【发布时间】:2018-11-20 17:02:41
【问题描述】:

我想使用已经在 Python 中 googleapis/api-common-protos 定义的 proto 文件。例如

syntax = "proto3";

package com.example.service.rev0;

import "google/protobuf/any.proto";
import "google/rpc/code.proto";

message GenericExcInfo {

    google.rpc.Code status_code = 1;

    string name = 2;

    string message = 3;

    repeated string stack_trace_entries = 4;

    string code_filename = 5;

    int32 code_lineno = 6;

    string code_name = 7;

    google.protobuf.Any arbitrary_info = 16;
}

如果安装了python包googleapis-common-protos,则相关的python模块(在本例中为google.rpc.code_pb2.Code)可用。但是由于没有安装 proto 文件,所以出现以下错误。

$ protoc --proto_path=. --python_out=. exc_info.proto
google/rpc/code.proto: File not found.
exc_info.proto: Import "google/rpc/code.proto" was not found or had errors.
exc_info.proto:14:5: "google.rpc.Code" is not defined.

如何在 Python 中使用这些 google proto 文件?我必须git clone repo 并在编译时包含每个 proto 文件吗?

【问题讨论】:

    标签: python google-api protocol-buffers


    【解决方案1】:

    解决了。我必须克隆它。

    https://github.com/googleapis/api-common-protos#using-these-protos

    如果您使用 protoc(或其他类似工具)来编译这些 protos 你自己,你可能需要一个本地副本。克隆这个 存储库到一个方便的位置并使用 --proto_path 指定 您机器上此存储库的根目录到编译器。

    【讨论】:

      猜你喜欢
      • 2018-03-11
      • 2021-03-13
      • 1970-01-01
      • 1970-01-01
      • 2021-12-30
      • 2023-01-17
      • 1970-01-01
      • 2019-12-10
      • 2020-07-05
      相关资源
      最近更新 更多