'Building protobuf using absolute path path python
I have protobuf definition in a folder like:
_ protocol
__ A
___ v1
____ a.proto
__ B
___ v1
____ b.proto
__ C
___ v1
____ c.proto
I want to built them and keep the same structure.
To do so I use this command : protoc -I=./my_folder/data/my_subfolder/protocol --python_out=./my_folder/data/build ./my_folder/data/my_subfolder/protocol/**/**/*.proto
It works I do get the same structure into my build folrder.
The problem is, I use a.proto into b.proto and into my b.pb2 the import looks like this:
from A.v1 import a_pb2 as alias_name
But I'd like it to be:
from my_folder.data.build.A.v1 import a_pb2 as alias_name
Is it possible ?
Thanks.
Solution 1:[1]
IIUC you may want to revise how you're invoking protoc.
NOTE be careful as there is a difference between Python and other languages in the use of the
packagedirective.packageis not used by Python but it is used by other languages. See Packages
You'll want to juggle (I'm not entirely sure how you want to remap the folders) --proto_path and --python-out directories
For example, the following will remove ${PWD}/protocol from the generated code's output location, using ${PWD}/build instead while preserve everything below:
protoc --proto_path=${PWD}/protocol \
--python_out=${PWD}/build \
${PWD}/my_folder/data/my_subfolder/protocol/**/**/*.proto
Source:
protocol
??? A
? ??? v1
? ??? a.proto
??? B
? ??? v1
? ??? b.proto
??? C
??? v1
??? c.proto
Output:
build
??? A
? ??? v1
? ??? a_pb2.py
??? B
? ??? v1
? ??? b_pb2.py
??? C
??? v1
??? c_pb2.py
And:
main.py:
from build.A.v1 import a_pb2 as some_alias
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 | DazWilkin |
