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?
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
).
© 2022 - 2024 — McMap. All rights reserved.