Skip to content
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

How to import .proto file in different package correctly? #4176

Closed
AlexLuya opened this issue Jan 17, 2018 · 2 comments
Closed

How to import .proto file in different package correctly? #4176

AlexLuya opened this issue Jan 17, 2018 · 2 comments
Labels

Comments

@AlexLuya
Copy link

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?

@arthur-tacca
Copy link

arthur-tacca commented Jan 17, 2018

(Note: I'm not a protobuf developer, I just came across your report.)

I think your inconsistent use of includes is the root of the problem. You have two choices, and you need to pick one and stick to it:

  1. Are the files in no particular package? In that case they are ./share.proto and ./detect.proto (as when using protoc -I. from within com/abc/depart/), and from Python use import share_pb2 and import detect_pb2.
  2. Or are they com.abc.<something> packages? In that case they are com/abc/protobuf/share.proto and com/abc/depart/detect.proto (as when using protoc -I/pathToSrcDir and in "com/abc/protobuf/share.proto"), and from Python use import com.abc.protobuf.share_pb2 and import com.abc.depart.detect_pb2.

I suspect you probably want the second one, so I'll just cover that. Then the import in detect.proto is fine, but you need to change your protoc commands like this:

cd /pathToSrcDir/
protoc -I. --python_out=. com/abc/protobuf/share.proto 
protoc -I. --python_out=. com/abc/depart/detect.proto

You could even merge the two commands:

cd /pathToSrcDir/
protoc -I. --python_out=. com/abc/protobuf/share.proto com/abc/depart/detect.proto

Make sure you have added /pathToSrc/ to your PYTHONPATH, NOT /pathToSrc/com/abc/protobuf.

By the way, you can always use a different directory for your protos and Python files. For example, if you have a proto/ directory and a src/ directory, use this command (and add /pathToProjectDir/src to your PYTHONPATH):

cd /pathToProjectDir/
protoc -Iproto --python_out=src proto/com/abc/protobuf/share.proto proto/com/abc/depart/detect.proto

@AlexLuya
Copy link
Author

AlexLuya commented Jan 17, 2018

Your solution works,thanks very much

ChrisCummins added a commit to ChrisCummins/phd that referenced this issue May 15, 2018
@xfxyjwf xfxyjwf closed this as completed Jun 23, 2018
ErikDeSmedt added a commit to ErikDeSmedt/greenlight that referenced this issue Feb 28, 2024
We are generating/compiling python bindings from `.proto`-files.

Before this commit the generated protobuf-files did not understand they
were a part of the `glclient`-module.

This resulted in broken impors which se fixed using `sed` in our
Makefile.

This commit improves the configuration and voids the need for hacks in
our `Makefile`.

For more info: See protocolbuffers/protobuf#4176
ErikDeSmedt added a commit to ErikDeSmedt/greenlight that referenced this issue Feb 28, 2024
We are generating/compiling python bindings from `.proto`-files.

Before this commit the generated protobuf-files did not understand they
were a part of the `glclient`-module.

This resulted in broken impors which se fixed using `sed` in our
Makefile.

This commit improves the configuration and voids the need for hacks in
our `Makefile`.

For more info: See protocolbuffers/protobuf#4176
ErikDeSmedt added a commit to ErikDeSmedt/greenlight that referenced this issue Feb 28, 2024
We are generating/compiling python bindings from `.proto`-files.

Before this commit the generated protobuf-files did not understand they
were a part of the `glclient`-module.

This resulted in broken impors which se fixed using `sed` in our
Makefile.

This commit improves the configuration and voids the need for hacks in
our `Makefile`.

For more info: See protocolbuffers/protobuf#4176
cdecker pushed a commit to ErikDeSmedt/greenlight that referenced this issue Mar 1, 2024
We are generating/compiling python bindings from `.proto`-files.

Before this commit the generated protobuf-files did not understand they
were a part of the `glclient`-module.

This resulted in broken impors which se fixed using `sed` in our
Makefile.

This commit improves the configuration and voids the need for hacks in
our `Makefile`.

For more info: See protocolbuffers/protobuf#4176
cdecker pushed a commit to Blockstream/greenlight that referenced this issue Mar 1, 2024
We are generating/compiling python bindings from `.proto`-files.

Before this commit the generated protobuf-files did not understand they
were a part of the `glclient`-module.

This resulted in broken impors which se fixed using `sed` in our
Makefile.

This commit improves the configuration and voids the need for hacks in
our `Makefile`.

For more info: See protocolbuffers/protobuf#4176
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants