pythonprotocol-buffersgrpc

Generate _pb2 python files with grpcio-tools with Decimal google type


I need to send python Decimal with gRPC. To do so i wrote this simplified proto file

syntax = "proto3";

package grpc.dec;

import "google/type/decimal.proto";

message Data {
  google.type.Decimal total = 1;
}

What i have installed into my venv

(venv) ➜ pip list
Package                  Version
------------------------ -------
googleapis-common-protos 1.63.0
grpcio                   1.63.0
grpcio-tools             1.63.0
pip                      23.0.1
protobuf                 4.25.3
setuptools               58.1.0

So i have site-packages/google/type/decimal.proto with Decimal message And grpcio-tools to run

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

But what i got when run this command

google/type/decimal.proto: File not found.
dec.proto:5:1: Import "google/type/decimal.proto" was not found or had errors.
dec.proto:210:3: "google.type.Decimal" is not defined.

I tried different variants and chatGPT advices, but nothing works. Please explain me how it should work


Solution

  • To use google/type/decimal.proto, you need to ensure that its repro googleapis is accessible (using --proto_path) when you invoke protoc.

    To do this:

    1. Clone the repo (you can store this wherever you'd like)
    GOOGLEAPIS="${PWD}"
    
    git clone \
    https://github.com/googleapis/googleapis \
    ${GOOGLEAPIS}
    
    1. Rerun protoc referencing --proto_path=${GOOGLEAPIS}:
    python \
    -m grpc_tools.protoc \
    --proto_path=${GOOGLEAPIS} \
    --proto_path=${PWD}/proto \
    --python_out=${PWD}/proto \
    --grpc_python_out=${PWD}/proto \
    --mypy_out=${PWD}/proto \
    ${PWD}/proto/*.proto
    

    The --proto_path (-I) flag provides protoc with a mechanism to find protocol buffer sources.

    In your case, it looks in each --proto_path for a folder that contains the following tree:

    .
    └── google
        └── type
            └── decimal.proto
    

    Because the import package is google/type/decimal.proto.

    Which is the conventional way to reflect decimal.proto's package google.type (NB the . in the package are folders on the filesystem).

    The googleapis repo includes this tree and thus it satsfies protoc.