0

I'm testing files generated by protobuf for use with python. The problem that I'm hitting is that protobuf is separating the generated python files in directories and then I have problems with the imports with python2.

Say, I have directories common and a, each with autogenerated (by protobuf) py files. If you take a look at one of the files in a, it has an import that says:

from common import x_pb2 as common_dot_x__pb2

So, I have another script that I'm creating to import the file in a. So.... if I try providing the parent (of common and a) to PYTHONPATH, I can't seem to find a way to import the script in a:

from a import blah

(blah.py being the autogenerated py file inside a) I get this:

ImportError: No module named a

So.... if I try adding the a directory to PYTHONPATH (and adjusting the import to only say import blah, then I start getting a problem importing from common (import statement in autogenerated blah.py file in a):

    from common import x_pb2 as common_dot_x__pb2
ImportError: No module named common

What should be the workaround for this?

eftshift0
  • 26,375
  • 3
  • 36
  • 60

1 Answers1

0

Ok..... got a workaround: In all of the directories where the autogenerated files are, add an empty init.py file. That way imports start behaving. Does anybody know of an easier way to do it? (And no w I have to see if this can be done automagically by protoc or with maven somehow).

eftshift0
  • 26,375
  • 3
  • 36
  • 60