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, a_sub.proto is importing a.proto, b.proto and c.proto.

protoc will generate the following import statements:

# a_sub_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:

# a_sub_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

# ...

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fix_protobuf_imports-0.1.4.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fix_protobuf_imports-0.1.4-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file fix_protobuf_imports-0.1.4.tar.gz.

File metadata

  • Download URL: fix_protobuf_imports-0.1.4.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.5

File hashes

Hashes for fix_protobuf_imports-0.1.4.tar.gz
Algorithm Hash digest
SHA256 a08ededb432a016b0e7b5d8964d913bdac7f8b49060b9cc2f5347485b94da8a7
MD5 36c6a9681391cce2ec48f0a21d608ba9
BLAKE2b-256 f1b757a22b8810168a89626c119669cb98c73fac1108f82e869f1224687c563d

See more details on using hashes here.

File details

Details for the file fix_protobuf_imports-0.1.4-py3-none-any.whl.

File metadata

File hashes

Hashes for fix_protobuf_imports-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 0dba546623d8351f4fe68c20e0c3fc7182c702f5791768fb41ca76a5962776e5
MD5 cc74476f23902aeb10bfa2922d0d80c5
BLAKE2b-256 c532b7e86f4776a6a5c02b5d24afa8dd6cc21059124b5381a85ba3f3dd7e3d78

See more details on using hashes here.

Release history Release notifications | RSS feed

0.1.7

2 files

0.1.6

2 files

0.1.5

2 files

This release

0.1.4 This release

2 files

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