Generate python protocol buffer files with nanopb

45 views
Skip to first unread message

Caleb

unread,
Feb 20, 2025, 6:30:08 AMFeb 20
to nanopb
Hi, 

Is it possible to use nanopb to also generate python protocol buffer files, i.e. _pb2.py? I am trying to add the custom option of --python-out= but i get the following error:
 Usage: nanopb_generator.py [options] file.pb ...

nanopb_generator.py: error: no such option: --python_out
For some added context I am running nanopb through platform.io and hoping to automatically generate both c and python protocol buffer files everytime i build the project. 

This is what my platformio .ini files looks like:
[env:teensy41]
platform = teensy
board = teensy41
framework = arduino
lib_deps =
    nanopb/Nanopb
custom_nanopb_protos =
    +<../proto/packet_config.proto>
custom_nanopb_options =
    --error-on-unmatched -I.pio/libdeps/teensy41/generator/proto/google/protobuf --python_out=.

build_flags = -D USB_RAWHID
Any help would be much appreciated!

Thanks,
Caleb

Petteri Aimonen

unread,
Feb 20, 2025, 6:38:14 AMFeb 20
to nan...@googlegroups.com
Hi,

Well, normally you would use protoc --python_out= separately for that.

But looks like this works to generate both outputs:

nanopb_generator --protoc-opt="--python_out=." test.proto

--
Petteri
> --
> You received this message because you are subscribed to the Google Groups "nanopb" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to nanopb+un...@googlegroups.com.
> To view this discussion visit https://groups.google.com/d/msgid/nanopb/cd76b1ca-3553-4aaa-bd7e-0650384f78b2n%40googlegroups.com.
> --
> You received this message because you are subscribed to the Google
> Groups "nanopb" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to [1]nanopb+un...@googlegroups.com.
> To view this discussion visit
> [2]https://groups.google.com/d/msgid/nanopb/cd76b1ca-3553-4aaa-bd7e-065
> 0384f78b2n%40googlegroups.com.
>
> References
>
> 1. mailto:nanopb+un...@googlegroups.com
> 2. https://groups.google.com/d/msgid/nanopb/cd76b1ca-3553-4aaa...@googlegroups.com?utm_medium=email&utm_source=footer


Caleb

unread,
Feb 20, 2025, 7:02:56 AMFeb 20
to nanopb
Thank you for such a quick reply, that has worked a treat!

Is there any particularly reason why it would normally be called separately? It seems in the situation where you have a pc running python sending messages to a microcontroller you would always want the corresponding pb files generated at the same time, and conveniently with the platformio plugin it only calls this command on build if the proto file has changed since last generation.

Im sure others will use platformio and nanopb in the future so for reference, if you are wanting to generate both nanopb-c and python protocol buffer files with the nanopb plugin in platformio the .ini file should look like this:
[env:teensy41]
platform = teensy
board = teensy41
framework = arduino
lib_deps =
    nanopb/Nanopb
custom_nanopb_protos =
    +<../proto/packet_config.proto>
custom_nanopb_options =
    --error-on-unmatched --protoc-opt="--python_out=<path_to_where_you_want_your_python_pb_files>"

build_flags = -D USB_RAWHID

The _.pb.c and _.pb.h files that nanopb generates get put in the .pio/build/teensy41/nanopb/generated-src folder, and can be included as you normally would in your project files.
Reply all
Reply to author
Forward
0 new messages