gpt4 book ai didi

python - GRPC_TOOLS.PROTOC 生成损坏的 pb2 python 文件

转载 作者:行者123 更新时间:2023-12-05 04:22:38 25 4
gpt4 key购买 nike

我正在尝试在 Python 中使用 gRPC 构建应用程序。我有以下目录布局:

service/

├─ auth/

│ ├─ auth_pb2_grpc.py

│ ├─ auth_pb2.py

│ ├─ auth.py

│ ├─ client.py

├─ flask/

├─ todo/

├─ app.py

├─ auth.proto/

├─ generate.sh/

├─ requirements.txt

我按照以下方式为 proto 文件设置代码:

syntax = "proto3";
package auth;


message RegisterRequest {
string username = 1;
string password = 2;
}

message RegisterResponse {
bool success = 1;
string message = 2;
}

message LoginRequest {
string username = 1;
string password = 2;
}

message LoginResponse {
bool success = 1;
string message = 2;
string token = 3;
}

message ValidateTokenRequest {
string token = 1;
}

message ValidateTokenResponse {
bool success = 1;
string message = 2;
}

service AuthService {
rpc Register(RegisterRequest) returns (RegisterResponse) {}
rpc Login(LoginRequest) returns (LoginResponse) {}
rpc ValidateToken(ValidateTokenRequest) returns (ValidateTokenResponse) {}
}

当我运行时

python -m grpc_tools.protoc -I. --python_out=./auth --grpc_python_out=./auth ./auth.proto

protobuf 将文件生成到 auth 子目录中。

看起来不错,不是吗?但是,当我运行 app.py 来测试新创建的文件时,它返回 -

ModuleNotFoundError: No module named 'auth_pb2'

这里是“auth_pb2”文件的内部:

    # -*- coding: utf-8 -*-
# Generated by the protocol buffer compiler. DO NOT EDIT!
# source: auth.proto
"""Generated protocol buffer code."""
from google.protobuf.internal import builder as _builder
from google.protobuf import descriptor as _descriptor
from google.protobuf import descriptor_pool as _descriptor_pool
from google.protobuf import symbol_database as _symbol_database
# @@protoc_insertion_point(imports)

_sym_db = _symbol_database.Default()




DESCRIPTOR = _descriptor_pool.Default().AddSerializedFile(b'\n\nauth.proto\x12\x04\x61uth\"5\n\x0fRegisterRequest\x12\x10\n\x08username\x18\x01 \x01(\t\x12\x10\n\x08password\x18\x02 \x01(\t\"4\n\x10RegisterResponse\x12\x0f\n\x07success\x18\x01 \x01(\x08\x12\x0f\n\x07message\x18\x02 \x01(\t\"2\n\x0cLoginRequest\x12\x10\n\x08username\x18\x01 \x01(\t\x12\x10\n\x08password\x18\x02 \x01(\t\"@\n\rLoginResponse\x12\x0f\n\x07success\x18\x01 \x01(\x08\x12\x0f\n\x07message\x18\x02 \x01(\t\x12\r\n\x05token\x18\x03 \x01(\t\"%\n\x14ValidateTokenRequest\x12\r\n\x05token\x18\x01 \x01(\t\"9\n\x15ValidateTokenResponse\x12\x0f\n\x07success\x18\x01 \x01(\x08\x12\x0f\n\x07message\x18\x02 \x01(\t2\xca\x01\n\x0b\x41uthService\x12;\n\x08Register\x12\x15.auth.RegisterRequest\x1a\x16.auth.RegisterResponse\"\x00\x12\x32\n\x05Login\x12\x12.auth.LoginRequest\x1a\x13.auth.LoginResponse\"\x00\x12J\n\rValidateToken\x12\x1a.auth.ValidateTokenRequest\x1a\x1b.auth.ValidateTokenResponse\"\x00\x62\x06proto3')

_builder.BuildMessageAndEnumDescriptors(DESCRIPTOR, globals())
_builder.BuildTopDescriptorsAndMessages(DESCRIPTOR, 'auth_pb2', globals())
if _descriptor._USE_C_DESCRIPTORS == False:

DESCRIPTOR._options = None
_REGISTERREQUEST._serialized_start=20
_REGISTERREQUEST._serialized_end=73
_REGISTERRESPONSE._serialized_start=75
_REGISTERRESPONSE._serialized_end=127
_LOGINREQUEST._serialized_start=129
_LOGINREQUEST._serialized_end=179
_LOGINRESPONSE._serialized_start=181
_LOGINRESPONSE._serialized_end=245
_VALIDATETOKENREQUEST._serialized_start=247
_VALIDATETOKENREQUEST._serialized_end=284
_VALIDATETOKENRESPONSE._serialized_start=286
_VALIDATETOKENRESPONSE._serialized_end=343
_AUTHSERVICE._serialized_start=346
_AUTHSERVICE._serialized_end=548
# @@protoc_insertion_point(module_scope)

正如我所见,问题出在 protobuf 生成中,因为 p2b 文件不知何故不完整。不知道为什么会发生...

还有一个类似的问题grpc_tools.protoc generates python files with broken imports , 但没有明确的答案。

能否请您指出我的代码中可能哪里出错了?

最佳答案

我在生成 p2b 文件时遇到了同样的问题。我已经能够通过降级下一个包来解决它:

  • Protobuf==3.14.0
  • grpcio-tools==1.33.2

我认为新版本的 grpcio-tools 和 protobuf 一定存在错误。我于 2020 年 12 月开始在 python 中使用 grpc,当时我使用的版本运行良好。

关于python - GRPC_TOOLS.PROTOC 生成损坏的 pb2 python 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73939276/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com