[Python] Getting imports right using grpc

Discussão em 'Python' iniciado por Stack, Agosto 22, 2025.

  1. Stack

    Stack Membro Participativo

    I have this "program" which works with Google protobufs. When creating Python code from the given compiler (protoc) it always says "DO NOT EDIT!". So I don't edit the generated code and I don't really want to (There must be a solution without fixing each file by myself every time the code is generated).

    Here is how my project is organized:

    |root:
    |---protos:
    | |---API:
    | | |---protoApiA.proto
    | | |---protoApiB.proto ...
    | |---common:
    | |---protoCommonA.proto ...
    |---src:
    | |---__init__.py
    | |---foo:
    | | |---__init__.py
    | | |---bar:
    | | | |---main.py
    | | | |---protos:
    | | | | |---API:
    | | | | | |---protoApiA_pb2.py
    | | | | | |---protoApiB_pb2-grpc.py ...
    | | | | |---common:
    | | | | | |---protoCommonA_pb2.py
    | | | | | |---....
    |---test_script.py


    protoApiA.proto imports from protoCommonA.proto.

    import "common/protoCommonA.proto";


    Which resolves into:

    from common import protoCommonA_pb2 as common_dot_protoCommonA__pb2


    After I created them using following PowerShell prompt:

    & py -3.10 -m grpc_tools.protoc -I./protos --python_out=./src/foo/bar/protos/ --grpc_python_out=./src/foo/bar/protos/ common/protoCommonA.proto API/protoApiA.proto API/protoApiB.proto


    So far so good. Here is what the init files contain:

    # root/src/__init__.py:
    from . import foo

    # root/src/foo/__init__.py:
    from .bar.main import ... #relevant functions for test_script.py


    The file main.py needs to use the proto files for some gRPC related stuff. The file test_script.py is a script which uses some functionalities from main.py mainly for testing.

    If I test the functionality over test_script.py it throws a "module not found"-error at protoApiA_pb2.py since it does not know about protoCommonA_pb2.py.

    So here is my question:
    How can I make this work, without editing the import statements in any of the automatically generated Python files?

    Continue reading...

Compartilhe esta Página