1

I'm using proto3 in my python project. Everything work good but when I generate classes from proto files import is not correct.

I have directory structure like this.

project/endpoints/protos -> image.proto

I want output files in.

project/endpoints/grpc -> generated classes from proto files

I'm using this command to generate classes from proto files.

python -m grpc.tools.protoc \
    --include_imports \
    --include_source_info \
    --proto_path=project/endpoints/protos \
    --python_out=project/endpoints/grpc \
    --grpc_python_out=project/endpoints/grpc \
    image.proto

I also tried it with python3 .... but same result.

it generate the files in project/endpoints/grpc but problem is the import is not correct. When I see import it look like.

import image_pb2 as image__pb2

But it should be like this.

import project.endpoints.grpc.image_pb2 as image__pb2

Can you please let me know how to generate import like this.

0

2 Answers 2

1

after much research

i found it in here

Solution in Mint:

sudo apt-get install 2to3

run to adjust the imports:

2to3 path/of/generated -w -n

so

import image_pb2

will change to

from . import image_pb2
Sign up to request clarification or add additional context in comments.

Comments

0

If you need:

from project.endpoints.grpc import image_pb2 as image__pb2

You should write this in the proto file:

import "project/endpoints/grpc/image.proto"

And then set the current directory or --proto_path to the parent of "project".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.