Convert all relative imports to absolute automatically in python

后端 未结 1 1078
半阙折子戏
半阙折子戏 2021-02-08 10:28

I am trying to structure my python 2.7 project (which entails several subdirectories) correctly. I have added __init__.py files on every level, and in the cas

相关标签:
1条回答
  • 2021-02-08 11:07

    You can use abs-imports https://github.com/MarcoGorelli/abs-imports :

    Installation

    pip install abs-imports
    

    Usage as a pre-commit hook

    See pre-commit for instructions

    Sample .pre-commit-config.yaml:

    -   repo: https://github.com/MarcoGorelli/abs-imports
        rev: v0.1.2
        hooks:
        -   id: abs-imports
    

    Command-line example

    $ cat mypackage/myfile.py
    from . import __version__
    $ abs_imports mypackage/myfile.py
    $ cat mypackage/myfile.py
    from mypackage import __version__
    

    Disclaimer: I'm the author of this little package

    0 讨论(0)
提交回复
热议问题