[Python] A generated protobuf python file is unable to locate another protobuf module it...

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

  1. Stack

    Stack Membro Participativo

    I have a project set up like this with protos/ as a top level directory and the project that actually builds it under services/common-py.

    ├── .venv
    ├── protos
    │ ├── common.proto
    │ ├── doodad.proto
    └── services
    └── common-py
    ├── my_common
    │ ├── __init__.py
    │ └── pb
    │ ├── __init__.py
    │ ├── common_pb2.py
    │ └── doodad_pb2.py
    ├── pyproject.toml
    ├── scripts
    │ └── gen_proto.sh
    ├── test_pb_construction.py


    The protos look like this:

    common.proto:

    syntax = "proto3";
    package io.mycompany.proto.model;
    message DoodadHeader {
    string id = 1;
    }


    and doodad.proto:

    syntax = "proto3";
    package io.mycompany.proto.model;
    import "common.proto";
    message Doodad {
    DoodadHeader header = 1;
    string doodad = 2;
    }


    I generate the protos like this:

    cd ./services/common-py
    python -m grpc_tools.protoc --python_out=my_common/pb --proto_path=/Users/paul/dev/protos_broken/protos common.proto
    python -m grpc_tools.protoc --python_out=my_common/pb --proto_path=/Users/paul/dev/protos_broken/protos doodad.proto


    I also have this __init__.py

    try:
    from .common_pb2 import DoodadHeader
    from .doodad_pb2 import Doodad
    except ImportError as e:
    raise e

    __all__ = [
    "DoodadHeader",
    "Doodad",
    ]


    I try to use Doodad and I get an error that doodad_pb2.py cannot import common_pb2.py. It looks like Doodad is found ok, but Doodad cannot find DoodadHeader

    from my_common.pb import Doodad, DoodadHeader
    def test_construction():
    doodad = Doodad(
    header=DoodadHeader(id="1"),
    doodad="doodad"
    )


    Traceback (most recent call last):
    File "/Users/paul/dev/protos_broken/services/common-py/test_pb_construction.py", line 3, in <module>
    from my_common.pb import Doodad, DoodadHeader
    File "/Users/paul/dev/protos_broken/services/common-py/my_common/pb/__init__.py", line 16, in <module>
    raise e
    File "/Users/paul/dev/protos_broken/services/common-py/my_common/pb/__init__.py", line 14, in <module>
    from .doodad_pb2 import Doodad
    File "/Users/paul/dev/protos_broken/services/common-py/my_common/pb/doodad_pb2.py", line 25, in <module>
    import common_pb2 as common__pb2
    ModuleNotFoundError: No module named 'common_pb2'


    Looking at that line 25 in doodad_pb2.py, there is this which seems odd:

    import common_pb2 as common__pb2


    What am I doing wrong here?

    Continue reading...

Compartilhe esta Página