代码之家  ›  专栏  ›  技术社区  ›  takaomag

如何在googleapis中使用proto文件在python中使用公共proto?

  •  0
  • takaomag  · 技术社区  · 6 年前

    我想使用proto文件,它已经在 googleapis/api-common-protos 在巨蟒中。例如

    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 在编译时包含所有proto文件?

    1 回复  |  直到 6 年前
        1
  •  0
  •   takaomag    6 年前

    解决了的。我必须克隆它。

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

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