Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

protobuf and python: can protoc generate __init__.py files?

Is there a way to ask protoc to generate empty __init__.py files in the directories that it uses to generated the .py files so that they can be used as modules?

like image 510
eftshift0 Avatar asked Mar 27 '19 17:03

eftshift0


1 Answers

I ran into the same issue. I'm surprised this was not solved yet by protoc. Also it seems the protoc generated Python code has other issues. See https://github.com/protocolbuffers/protobuf/issues/881

So the answer is at the moment no. Protoc cannot generate __init__.py files.

One option may be to use https://github.com/danielgtaylor/python-betterproto to generate Python code from .proto (our proto definitions are also structured in subfolders).

The other option I'm currently looking into is to add an empty init.py file manually with a bash script: Creating empty file in all subfolders

find <path-to-generated-python-proto> -type d -exec touch {}/__init__.py \;

Edit: I entered the empty __init__.py to all folders of generated code and then I can package Python modules using find_packages (as that only finds modules with __init__.py).

like image 163
Ondrej Burkert Avatar answered Oct 20 '22 23:10

Ondrej Burkert