The repro is somewhat unclear.

The package structure is uses `abc` but the protobuf sources use `adc`; I 
assume both should be the same.

In `share.proto`, `message Test` contains no fields and fields contain 
values, so the `Test` message contains no values and isn't useful.

The `detect.proto` contains no messages and so isn't useful either. 

If instead the following structure is used:

```
.
├── com
│   └── abc
│       ├── depart
│       │   └── detect.proto
│       └── protobuf
│           └── share.proto
└── main.py
```
And:
`detect.proto`:
```
syntax = "proto3";

package com.abc.depart;

import "com/abc/protobuf/share.proto";

message X{
    com.abc.protobuf.Test test = 1;
}
```
And:
`share.proto`:
```
syntax = "proto3";

package com.abc.protobuf;

message Test{
    string value = 1;
}
```

Because, there's a single protopath (`${PWD}` or `.`), we can use one 
`protoc` command to compile to Python sources:

```bash
protoc \
--proto_path=${PWD} \
--python_out=${PWD} \
--pyi_out=${PWD} \
${PWD}/com/abc/depart/detect.proto \
${PWD}/com/abc/protobuf/share.proto
```
and e.g. `main.py`:
```python3
from com.abc.depart import detect_pb2

x = detect_pb2.X()
x.test.value = "foo"

print(x)
```
Yields:

```console
test {
  value: "foo"
}
```
On Thursday, September 14, 2023 at 9:32:17 PM UTC-7 Charlie Tian wrote:

> "I've encountered the same issue. Have you found a solution?
>
> 在2018年1月17日星期三 UTC+8 15:52:17<Alexander Luya> 写道:
>
>> I have two packages like this
>>
>>     com.abc.
>>              protobuf.
>>                         share.proto
>>              depart.
>>                         detect.proto 
>>
>> and the conent of  share.proto like this:
>>
>>     syntax = "proto3";
>>     package com.adc.protobuf;
>>     message Test{}
>>        
>> and the content of detect.proto like this:
>>
>>     syntax = "proto3";
>>     package com.adc.depart;
>>     import "com/abc/protobuf/share.proto"
>>
>> and compile share.proto in it's dir like this:
>>
>>     protoc -I=. --python_out=. share.proto
>>
>> then compile detect.proto in it's dir like this:
>>
>>     protoc -I=/pathToSrcDir/ -I=. --python_out=. detect.proto 
>> and
>>     
>>     pathToSrcDir has been added to PYTHONPATH
>>
>> all compilations work fine,but when run a python script which 
>>
>>     from com.abc.depart import detect_pb2
>>
>> got this error   
>>
>>     TypeError: Couldn't build proto file into descriptor pool!
>>     Invalid proto descriptor for file "detect.proto":
>>       detect.proto: Import "com/abc/protobuf/share.proto" has not been 
>> loaded.
>>       com.abc.depert.XClass.ymethod: "com.abc.protobuf.Test" seems to be 
>> defined in "share.proto", which is not imported by "detect.proto".  To use 
>> it here, please add the necessary import.
>>
>> How to solve this import problem?
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to protobuf+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/protobuf/2645b186-2943-411d-b2f9-bd26974bae44n%40googlegroups.com.

Reply via email to