https://github.com/crflynn/protobuf-init
python protoc plugin for generating __init__.py (init) files
https://github.com/crflynn/protobuf-init
init protobuf protoc protocol-buffers python
Last synced: 8 months ago
JSON representation
python protoc plugin for generating __init__.py (init) files
- Host: GitHub
- URL: https://github.com/crflynn/protobuf-init
- Owner: crflynn
- License: mit
- Created: 2021-12-11T07:17:37.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2022-12-07T20:37:23.000Z (about 3 years ago)
- Last Synced: 2025-03-28T23:43:23.211Z (9 months ago)
- Topics: init, protobuf, protoc, protocol-buffers, python
- Language: Python
- Homepage:
- Size: 38.1 KB
- Stars: 1
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# protobuf-init
To install:
```bash
pip install protobuf-init
```
This package will create `__init__.py` files when compiling `*.proto` files. Optionally, it will create relative imports from generated `*_pb.py`, `*_pb_grpc.py`, and `*_grpc.py` files from `protobuf`, `grpcio`, and `grpclib` packages, respectively.
Using the `protos` folder of this project as an example, the following command will generate the contents of the `example` package, also in this project (assuming `grpcio-tools` is installed):
```bash
export PROTO_PATH=./protos
export GEN_PATH=.
python -m grpc_tools.protoc \
--python_out=$GEN_PATH \
--mypy_out=$GEN_PATH \
--init_python_out=$GEN_PATH \
--init_python_opt=imports=protobuf+grpcio+grpclib \
--grpc_python_out=$GEN_PATH \
--grpclib_python_out=$GEN_PATH \
--proto_path=$PROTO_PATH
$(find $PROTO_PATH -name '*.proto')
```
The `--init_python_out=$GEN_PATH` flag indicates to call the plugin to create the init files.
The `--init_python_opt=imports=protobuf+grpcio+grpclib` indicates which relative imports to include in the init files. Allowed options are `protobuf`, `grpcio`, `grpclib`, separated by `+`. (Note that both grpcio and grpclib generate `Stub` objects which would collide in the init file.)