A script to fix relative imports (from and to nested sub-directories) within compiled `*_pb2.py` Protobuf files.
Project description
fix-protobuf-imports
This script will fix relative imports (from and to nested sub-directories) within compiled *pb2.py
and *pb2.pyi
Protobuf files/modules generated from protoc --python_out --mypy_out
:
fix-protobuf-imports /path/to/python_out/dir
When do I need to fix my imports?
E.g. you might have the following file/module structure:
./
a_pb2.py
b_pb2.py
./sub/
c_pb2.py
./nested/
d_pb2.py
__init__.py
__init__.py
__init__.py
Now assume, c.proto
is importing a.proto
, b.proto
and d.proto
.
protoc
will generate the following import statements for c_pb2.py
:
# c_pb2.py
from google.protobuf import descriptor as _descriptor
import a_pb2 as a__pb2
import b_pb2 as b__pb2
from sub.nested import d_pb2 as sub_dot_nested__d__pb2
# ...
Using these modules will not work under Python 3, as the imports are not relative. As it can get quite cumbersome to fix these issues, this script will convert the imports automatically:
fix-protobuf-imports /path/to/python_out/dir
This will result in the following working imports:
# c_pb2.py
from google.protobuf import descriptor as _descriptor
from .. import a_pb2 as a__pb2
from .. import b_pb2 as b__pb2
from ..sub.nested import d_pb2 as sub_dot_nested__d__pb2
# ...
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for fix_protobuf_imports-0.1.5.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | b33cfe334a0c56be5303e761e65eda49be31f9184ad5a6f46f58cb77c28a2bd4 |
|
MD5 | b2943b3df57f9dab893c855c2f990278 |
|
BLAKE2b-256 | c38eae02c4e57a37345d4e0052afb2fe9bb90f5dd47618a4f0189bb16a7611ba |
Hashes for fix_protobuf_imports-0.1.5-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ae25904593523419b19220a4ce1ac1ef2f41789424bda1fd9baf95a35c4e00e7 |
|
MD5 | 199a756dc6a0afc050e8ada6fda2db26 |
|
BLAKE2b-256 | fb4a4e15296837d7eb9eb1db6ab6e24d0d4954668674ab758ba04e613509b7e5 |