Protocol Buffer 导入解析 [英] Protocol Buffer import resolution

查看:118
本文介绍了Protocol Buffer 导入解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读这个相当长的问题之前,我提出了一个错误

我的本​​地项目目录包含三个 .proto 文件:

  1. example.proto
  2. google/api/annotations.proto
  3. google/api/http.proto

...或者作为一棵树:

|____google||____api|||____annotations.proto|||____http.proto|____example.proto

目标(或'out')目录也被添加,准备接收生成的python文件:

|____generated_pb2||____谷歌|||____api

我的完整项目目录结构是:

  • example.proto
  • google/api/annotations.proto
  • google/api/http.proto
  • generated_pb2/google/api

...或者作为一棵树:

|____example.proto|____谷歌||____api|||____annotations.proto|||____http.proto|____生成的_pb2||____谷歌|||____api

有了这个,我可以编译我的原型(为了可读性而添加了格式):

python -m grpc_tools.protoc--python_out=generated_pb2--grpc_python_out=generated_pb2-I ~/protoc/include/google/protobuf-我/谷歌/api示例.proto

分解:

  • generated_pb2 - 生成的 python 文件和 gprc 文件的目的地.
  • ~/protoc/include/google/protobuf - protoc 二进制文件附带的常见 protos 的位置,自 annotations.proto 取决于 google/protobuf/descriptor.proto.
  • google/api - annotations.protohttp.proto
  • 的位置

这会编译 example.proto 给出:

  • generated_pb2/example_pb2.py
  • generated_pb2/example_pb2_gprc.py

但是generated_pb2/example_pb2.py的第一行导入了annotations.proto的生​​成文件:

 from google.api import annotations_pb2 as google_dot_api_dot_annotations__pb2

此文件不存在.没问题,我会单独编译annotations.proto:

python -m grpc_tools.protoc--python_out=generated_pb2/google/api--grpc_python_out=generated_pb2/google/api-I ~/protoc/include/google/protobuf-我谷歌/api annotations.proto

分解:

  • generated_pb2/google/api - 生成的 python 文件和 gprc 文件的目的地.
  • ~/protoc/include/google/protobuf - protoc 二进制文件附带的常见 protos 的位置,自 annotations.proto 取决于 google/protobuf/descriptor.proto.
  • google/api - annotations.proto 所依赖的 http.proto 的位置.

不幸的是,此时我收到一个错误:

google/api/http.proto:找不到文件.annotations.proto:未找到导入google/api/http.proto"或有错误.annotations.proto:30:3: "HttpRule" 未定义.

我猜这是因为 annotations.protogoogle/api 中寻找 http.proto :

syntax = "proto3";包 google.api;导入google/api/http.proto";导入google/protobuf/descriptor.proto";

然而,目前还不清楚如何解决这种依赖关系.protoc --help 记录 -I 标志:

-IPATH, --proto_path=PATH 指定要搜索的目录进口.可以多次指定;将按顺序搜索目录.如果不给定,使用当前工作目录.

如何解决annotations.proto依赖的http.proto?

解决方案

试试这个:pip install googleapis-common-protos.遇到同样的错误,用这个方法解决了.

Before reading through this rather long question, I've raised a bug https://github.com/GoogleCloudPlatform/python-docs-samples/issues/1103.

The documentation for Proto Packages and Name Resolution states

You can use definitions from other .proto files by importing them. To import another .proto's definitions, you add an import statement to the top of your file.

My example.proto depends on annotations.proto to transcode HTTP/JSON to gRPC. This is a trivial example, but note I use the import path from the googleapis/google/api Git repo (i.e. google/api/annotations.proto):

syntax = "proto3";
import "google/api/annotations.proto";

message MyExample {
  // Message definition here.
}

Note, annotations.proto depends on http.proto - they are siblings in the same package (googleapis/google/api)

My local project directory contains three .proto files:

  1. example.proto
  2. google/api/annotations.proto
  3. google/api/http.proto

...or as a tree:

|____google
| |____api
| | |____annotations.proto
| | |____http.proto
|____example.proto

The target (or, 'out') directories are added too, ready to receive the generated python files:

|____generated_pb2
| |____google
| | |____api

There for my complete project directory structure is:

  • example.proto
  • google/api/annotations.proto
  • google/api/http.proto
  • generated_pb2/google/api

...or as a tree:

|____example.proto
|____google
| |____api
| | |____annotations.proto
| | |____http.proto
|____generated_pb2
| |____google
| | |____api

With this in place I can compile my protos (formatting added for readability):

python -m grpc_tools.protoc
  --python_out=generated_pb2
  --grpc_python_out=generated_pb2
  -I ~/protoc/include/google/protobuf
  -I /google/api
  example.proto

Breaking this down:

  • generated_pb2 - Destination for generated python files and gprc files.
  • ~/protoc/include/google/protobuf - Location of common protos shipped with protoc binary, needed since annotations.proto depends on google/protobuf/descriptor.proto.
  • google/api - Location of annotations.proto and http.proto

This compiles example.proto giving:

  • generated_pb2/example_pb2.py
  • generated_pb2/example_pb2_gprc.py

However the first line of generated_pb2/example_pb2.py imports the generated files for annotations.proto:

from google.api import annotations_pb2 as google_dot_api_dot_annotations__pb2

This file doesn't exist. No problem, I'll compile annotations.proto separately:

python -m grpc_tools.protoc
  --python_out=generated_pb2/google/api
  --grpc_python_out=generated_pb2/google/api
  -I ~/protoc/include/google/protobuf
  -I google/api annotations.proto

Breaking this down:

  • generated_pb2/google/api - Destination for generated python files and gprc files.
  • ~/protoc/include/google/protobuf - Location of common protos shipped with protoc binary, needed since annotations.proto depends on google/protobuf/descriptor.proto.
  • google/api - Location of http.proto upon which annotations.proto depends.

Unfortunately I get an error at this point:

google/api/http.proto: File not found.
annotations.proto: Import "google/api/http.proto" was not found or had errors.
annotations.proto:30:3: "HttpRule" is not defined.

I guess this is because annotations.proto looks for http.proto in google/api:

syntax = "proto3";
package google.api;

import "google/api/http.proto";
import "google/protobuf/descriptor.proto";

However it's unclear how this dependency is resolved. protoc --help documents the -I flag:

-IPATH, --proto_path=PATH   Specify the directory in which to search for
                            imports.  May be specified multiple times;
                            directories will be searched in order.  If not
                            given, the current working directory is used.

How can http.proto upon which annotations.proto depends, be resolved?

解决方案

Try this : pip install googleapis-common-protos. I encountered the same error and solved it using this method.

这篇关于Protocol Buffer 导入解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆