'How to make protoc generated python stub file to include package name in import

I use the protoc tool, from grpc-tools package (ver. 1.43.0) with python 3.8.10 to generate the data types and client and server stubs for my proto3 service. The generated file containing the stubs (ending in '_grpc.py') imports the generated file containing the message data types using an import statement with no package name: 'import <my_service_name>_pb2.py'. If I understand correctly, in order for this import to work properly in python 3 when the generated code is being included in a package, this import needs to include the name of the package containing it: 'from <my_containing_package> import <my_service_name>_pb2.py'. Is there a way to make protoc generate this import with the package name I require?

P.S. I'm currently using sed to post edit the generated file to add the package name, so only interested, here, in a way to make protoc generate the correct form for python 3 in the first place.



Solution 1:[1]

TL; DR; use https://github.com/cpcloud/protoletariat.

From that tool, you can follow the discussion on the protocolbuffers issue, which basically clarifies that the intended use is to pump all your stubs in a flat source directory.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 xtofl