Coder Social home page Coder Social logo

mjspeck / fix_protobuf_imports Goto Github PK

View Code? Open in Web Editor NEW

This project forked from technokrat/fix_protobuf_imports

0.0 1.0 0.0 6 KB

A script to fix relative imports (from and to nested sub-directories) within compiled `*_pb2.py` Protobuf files.

License: MIT License

Python 100.00%

fix_protobuf_imports's Introduction

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

# ...

fix_protobuf_imports's People

Contributors

mjspeck avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.