views:

28

answers:

2

I have my directory structure like this:

root/
  sift/
    __init__.py
    sift_descriptors.proto
    sift_descriptors_pb2.py
  project/
    __init__.py
    filtered_descriptors.proto
    filtered_descriptors_pb2.py
    filtered_descriptors_test.py

The root directory is in my $PYTHONPATH.

I build root/sift/sift_descriptors_pb2.py using protoc --python_out=./ sift_descriptors.proto

I build root/project/filtered_descriptors_pb2.py using /cs/public/lib/pkg/protobuf/bin/protoc --proto_path=../sift --proto_path=./ --python_out=./ filtered_descriptors.proto

In filtered_descriptors.proto, I use import "sift_descriptors.proto"

The problem is that in filtered_descriptors_pb2.py (produced by protoc), there's a statement that just does this bare import: import sift_descriptors_pb2, without reference via the module name as would be needed: from sift import sift_descriptors_pb2.

What am I doing wrong?

+1  A: 

You don't add .py to the import statement: "from sift import sift_descriptors_pb2"

Jason Baker
Oops, that was a transcription error. I fixed my question.
Sancho
Thanks! But, just to be clear, my question still stands. I had just asked it incorrectly at first.
Sancho
+1  A: 

I fixed it!

The solution was to use import "sift/sift_descriptors.proto" in filtered_descriptors.proto, and then point protoc to --proto_path=../ instead of --proto_path=../sift.

Then, protoc generates python code that does the import as import sift.sift_descriptors_pb2.

Sancho
Talking with the maintainers of protocol buffers, --proto_path is supposed to point to the root of the package hierarchy and all imports should be fully qualified. Not well described in the documentation, but they'll fix it up :)
Sancho