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.pyb_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
# ...
Development
This project uses the Python project managment tool poetry.
- Install Poetry
curl -sSL https://install.python-poetry.org | python3 - --preview
- Install dependencies
poetry install
- Test script
poetry run fix-protobuf-imports --help
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fix_protobuf_imports-0.1.7.tar.gz.
File metadata
- Download URL: fix_protobuf_imports-0.1.7.tar.gz
- Upload date:
- Size: 4.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.2.0rc2 CPython/3.10.7 Windows/10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
19e21b909e13b034906414199fc32011e6d161ffbb900935eb4df7ef7fa84c87
|
|
| MD5 |
08f740dacba9611ac14d948599caf027
|
|
| BLAKE2b-256 |
c7c8b5464411051fc876aac32323ceeafe3308ec619b6ed66a2dc36f759e1811
|
File details
Details for the file fix_protobuf_imports-0.1.7-py3-none-any.whl.
File metadata
- Download URL: fix_protobuf_imports-0.1.7-py3-none-any.whl
- Upload date:
- Size: 4.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.2.0rc2 CPython/3.10.7 Windows/10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d0363779783db5ebab586f4e34cddfd96ba8fbb5c39e7d04bcb743fc9a1222d0
|
|
| MD5 |
fb6ef8ffbbd829bbe6ea54f26629861c
|
|
| BLAKE2b-256 |
c2f858c189f06800aebe813bcc33a52ffc9c19260e2642b0ca0e39d69cf202c0
|