Sort Python module imports alphabetically

I'm collaborating on Django and the modules are imported in alphabetical order, separated by standard library, third party, and oleore modules! When I was told, I was looking for something like gofmt from golang that would do it for me.

https://github.com/timothycrosley/isort

Very easy to deploy

$ pip install isort

After installation with

$ isort spam.py

So, it sorts and overwrites it. Let's do it before committing.

You can change the behavior by creating a configuration file ~ / .isort.cfg.

isort.cfg


[settings]
line_length=120                                                                                                                                                                   
multi_line_output=3

It allows up to 120 characters per line and sets the line feed method when importing multiple lines. In this case, if 120 characters are exceeded, a line break will occur as shown below.

from third_party import (
    lib1,
    lib2,
    lib3,
    lib4,
)

Click here if you want to do it on Vim instead of the shell

https://github.com/fisadev/vim-isort

If you are using NeoBundle etc., just write it in vimrc.

vimrc


NeoBundle 'fisadev/vim-isort'

This will sort by : Isort in command mode or<C-i>in visual mode by selecting the line you want to sort.

Recommended Posts

Sort Python module imports alphabetically
[Python] Sort
Python # sort
Python module import
Python collections module
Python module (Python learning memo ④)
Create a Python module
Bubble sort in Python
Python self-made class sort
python original module import
[Memo] Python3 list sort
Python sort cheat sheet
Custom sort in Python3
[Python] Sort collection types
How Python module import works
[Python] ModuleNotFoundError: No module named'urlparse'
Naturally sort Path in Python
About the Python module venv
Python basic dict sort order
Automatic update of Python module
Sort huge files with python
python in mongodb in descending sort
Python beginners organize bubble sort
Ruby, Python Module Installation Guide
Python debug and test module
Sort by date in python
About Python sort () and reverse ()
[Python] Random processing (create, select, sort)
[Python] logging in your own module
[Python] Sort iterable by multiple conditions
Python error: ModuleNotFoundError: No module named'flask'
Cooperation between python module and API
[Code] Module and Python version output
Sort names with Python's random module
Use blender as a python module
Sort large text files in Python
Python3 socket module and socket communication flow
Python unittest module execution in vs2017
[Python] One-liner Stalin sort with 50 characters
Python My Number verification module released
Try using the Python Cmd module
Python executable file conversion module comparison 2
Looking for location for mercurial python module
Master the weakref module in Python