Skip to main content

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

# ...

Development

This project uses the Python project managment tool poetry.

  1. Install Poetry
  curl -sSL https://install.python-poetry.org | python3 - --preview
  1. Install dependencies
  poetry install
  1. Test script
  poetry run fix-protobuf-imports --help

Metadata

Release files for fix-protobuf-imports 0.1.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fix-protobuf-imports 0.1.7
File Size Uploaded Provenance Actions
fix_protobuf_imports-0.1.7.tar.gz 4.2 kB Without provenance Details for fix_protobuf_imports-0.1.7.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for fix-protobuf-imports 0.1.7
File Interpreter ABI Platform Actions
fix_protobuf_imports-0.1.7-py3-none-any.whl Python 3 none any Details for fix_protobuf_imports-0.1.7-py3-none-any.whl

Total release size: 8.8 kB

Release files / fix_protobuf_imports-0.1.7.tar.gz

Download URL fix_protobuf_imports-0.1.7.tar.gz
Size 4.2 kB
Tags Source
SHA-256 checksum
How to use checksums
19e21b909e13b034906414199fc32011e6d161ffbb900935eb4df7ef7fa84c87
BLAKE2b-256 checksum
How to use checksums
c7c8b5464411051fc876aac32323ceeafe3308ec619b6ed66a2dc36f759e1811
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.2.0rc2 CPython/3.10.7 Windows/10

Release files / fix_protobuf_imports-0.1.7-py3-none-any.whl

Download URL fix_protobuf_imports-0.1.7-py3-none-any.whl
Size 4.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d0363779783db5ebab586f4e34cddfd96ba8fbb5c39e7d04bcb743fc9a1222d0
BLAKE2b-256 checksum
How to use checksums
c2f858c189f06800aebe813bcc33a52ffc9c19260e2642b0ca0e39d69cf202c0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.2.0rc2 CPython/3.10.7 Windows/10

Security for fix-protobuf-imports 0.1.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page