-
Notifications
You must be signed in to change notification settings - Fork 15.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
python_out files seem to forget about --proto_path #4614
Comments
I think we do not have plan to support relative imports. See #1491 for more discussion |
I don't see the connection to relative imports. I have only, and only wish to have, absolute imports; looking at that ticket, it seems like prima facie to be a different issue. There isn't a problem with Python finding the module I want to import; IMO the problem is with protoc's codegen. |
That is, when I run this command: (tmp) ~/tmp/y 12:58:04 $ find . -name '*.proto' | xargs protoc --python_out=. --proto_path=$HOME/tmp/x/pb --proto_path=.
and The failure happens in this instantiation, in DESCRIPTOR = _descriptor.FileDescriptor(
name='pb/tmp/y/pb/y.proto',
package='tmp.y.pb',
syntax='proto3',
serialized_pb=_b('\n\x13pb/tmp/y/pb/y.proto\x12\x08tmp.y.pb\x1a\x10tmp/x/pb/x.proto\"$\n\x06Thing2\x12\x1a\n\x01t\x18\x01 \x01(\x0b\x32\x0f.tmp.x.pb.Thingb\x06proto3')
,
dependencies=[tmp_dot_x_dot_pb_dot_x__pb2.DESCRIPTOR,]) By this point, we have already executed the line |
Why not use --python_out for your x.proto to produce x_pb2.py under y dictionary? If your .proto files are under different path, use --proto_path to find the import files and --python_out to make sure the generated _pb2 files are under same path Plus we will appreciate if you can change your structure to less complicated graph next time like: ├── x
│ └── pb
│ ├── __init__.py
│ ├── x_pb2.py
│ └── x.proto
└── y
└── pb
├── __init__.py
├── y_pb2.py
└── y.proto |
Well, for one thing:
(You might just ask "why not have And even if it did work, it would require copying the generated file into every directory that might want to refer to it, which I don't think is either practical or good design. Possibly, of course, you had something else in mind. If so, I would appreciate it if you could tell me what it was that you did have in mind, rather than suggesting that I do something unspecified, and leaving me to discover whether or not the thing I think you might have meant works. The directory structure I've given is not that hard to reproduce, nor is it unrealistic. I can even give you a tarball containing all the files, including the |
Can you double check what file do you get after : This should produce a y_pb2.py under $HOME/tmp/y/ not $HOME/tmp/y/pb/tmp/y Please understand we always ask users try to minimize how to reproduce the error to help us better understanding what the real issue is. |
|
Anyway, here's a tarball. If you unpack it and source |
@bwo @anandolee I am running into this issue as well and am curious if there is any known workaround. I added |
We're literally using |
Closing this to dedupe against #10329 which tracks the same issue. |
Here's what I mean.
suppose you have this directory structure:
The contents of the
.proto
files are as follows:The reason that
y.proto
importsx.proto
with the path"tmp/x/pb/x.proto"
is that when both of these packages are installed,x_pb2
will be importable astmp.x.pb.x_pb2
, and (to my astonishment) the only way to getprotoc
to generate such an import path in python is to import it with that path in the proto file. This means that we generate the*_pb2.py
files with some--proto_path
shenanigans:This works, in the sense that
protoc
doesn't complain about anything, it finds all the necessary files, and it generates Python code. But in another, more accurate, sense, it does not work:That is, even though the protoc compiler made use of the
--proto_path
options in order to find and generate its output, the output it generated is not aware of the paths provided: it's precisely becausepb/tmp/y/pb/y.proto
doesn't importpb/tmp/x/pb/x.proto
, but rather simplytmp/x/pb/x.proto
, that we passed the--proto_path
option to the compiler!The text was updated successfully, but these errors were encountered: