| 1 |
# Introducing isort 5 |
| 2 |
|
| 3 |
[](https://isort.readthedocs.io/) |
| 4 |
|
| 5 |
isort 5.0.0 is the first major release of isort in over five years and the first significant refactoring of isort since it was conceived more than ten years ago. |
| 6 |
It's also the first version to require Python 3 (Python 3.6+ at that!) to run - though it can still be run on source files from any version of Python. |
| 7 |
This does mean that there may be some pain with the upgrade process, but we believe the improvements will be well worth it. |
| 8 |
|
| 9 |
[Click here for an attempt at full changelog with a list of breaking changes.](../changelog.rst) |
| 10 |
|
| 11 |
[Using isort 4.x.x? Click here for the isort 5.0.0 upgrade guide.](../upgrade_guides/5.0.0.md) |
| 12 |
|
| 13 |
[Try isort 5 right now from your browser!](../quick_start/0.-try.md) |
| 14 |
|
| 15 |
So why the massive change? |
| 16 |
|
| 17 |
## Profile support |
| 18 |
``` |
| 19 |
isort --profile black . |
| 20 |
isort --profile django . |
| 21 |
isort --profile pycharm . |
| 22 |
isort --profile google . |
| 23 |
isort --profile open_stack . |
| 24 |
isort --profile plone . |
| 25 |
isort --profile attrs . |
| 26 |
isort --profile hug . |
| 27 |
``` |
| 28 |
|
| 29 |
isort is very configurable. That's great, but it can be overwhelming, both for users and for the isort project. isort now comes with profiles for the most common isort configurations, |
| 30 |
so you likely will not need to configure anything at all. This also means that as a project, isort can run extensive tests against these specific profiles to ensure nothing breaks over time. |
| 31 |
|
| 32 |
## Sort imports **anywhere** |
| 33 |
|
| 34 |
```python3 |
| 35 |
import a # <- These are sorted |
| 36 |
import b |
| 37 |
|
| 38 |
b.install(a) |
| 39 |
|
| 40 |
import os # <- And these are sorted |
| 41 |
import sys |
| 42 |
|
| 43 |
|
| 44 |
def my_function(): |
| 45 |
import x # <- Even these are sorted! |
| 46 |
import z |
| 47 |
``` |
| 48 |
|
| 49 |
isort 5 will find and sort contiguous section of imports no matter where they are. |
| 50 |
It also allows you to place code in-between imports without any hacks required. |
| 51 |
|
| 52 |
## Streaming architecture |
| 53 |
|
| 54 |
```python |
| 55 |
import a |
| 56 |
import b |
| 57 |
|
| 58 |
... |
| 59 |
# more code! |
| 60 |
``` |
| 61 |
isort has been refactored to use a streaming architecture. This means it can sort files of *any* size (even larger than the Python interpreter supports!) without breaking a sweat. |
| 62 |
It also means that even when sorting imports in smaller files, it is faster and more resource-efficient. |
| 63 |
|
| 64 |
## Consistent behavior across **all** environments |
| 65 |
|
| 66 |
Sorting the same file with the same configuration should give you the same output no matter what computer or OS you are running. Extensive effort has been placed around refactoring |
| 67 |
how modules are placed and how configuration files are loaded to ensure this is the case. |
| 68 |
|
| 69 |
|
| 70 |
## Cython support |
| 71 |
|
| 72 |
```python3 |
| 73 |
cimport ctime |
| 74 |
from cpython cimport PyLong_FromVoidPtr |
| 75 |
from cpython cimport bool as py_bool |
| 76 |
from cython.operator cimport dereference as deref |
| 77 |
from cython.operator cimport preincrement as preinc |
| 78 |
from libc.stdint cimport uint64_t, uintptr_t |
| 79 |
from libc.stdlib cimport atoi, calloc, free, malloc |
| 80 |
from libc.string cimport memcpy, strlen |
| 81 |
from libcpp cimport bool as cpp_bool |
| 82 |
from libcpp.map cimport map as cpp_map |
| 83 |
from libcpp.pair cimport pair as cpp_pair |
| 84 |
from libcpp.string cimport string as cpp_string |
| 85 |
from libcpp.vector cimport vector as cpp_vector |
| 86 |
from multimap cimport multimap as cpp_multimap |
| 87 |
from wstring cimport wstring as cpp_wstring |
| 88 |
``` |
| 89 |
|
| 90 |
isort 5 adds seamless support for Cython (`.pyx`) files. |
| 91 |
|
| 92 |
## Action Comments |
| 93 |
|
| 94 |
```python3 |
| 95 |
import e |
| 96 |
import f |
| 97 |
|
| 98 |
# isort: off <- Turns isort parsing off |
| 99 |
|
| 100 |
import b |
| 101 |
import a |
| 102 |
|
| 103 |
# isort: on <- Turns isort parsing back on |
| 104 |
|
| 105 |
import c |
| 106 |
import d |
| 107 |
``` |
| 108 |
|
| 109 |
isort 5 adds support for [Action Comments](../configuration/action_comments.md) which provide a quick and convenient way to control the flow of parsing within single source files. |
| 110 |
|
| 111 |
|
| 112 |
## First class Python API |
| 113 |
|
| 114 |
```python3 |
| 115 |
import isort |
| 116 |
|
| 117 |
isort.code(""" |
| 118 |
import b |
| 119 |
import a |
| 120 |
""") == """ |
| 121 |
import a |
| 122 |
import b |
| 123 |
""" |
| 124 |
``` |
| 125 |
|
| 126 |
isort now exposes its programmatic API as a first-class citizen. This API makes it easy to extend or use isort in your own Python project. You can see the full documentation for this new API [here](../reference/isort.rst). |
| 127 |
|
| 128 |
## Solid base for the future |
| 129 |
|
| 130 |
A major focus for the release was to give isort a solid foundation for the next 5-10 years of the project's life. |
| 131 |
isort has been refactored into functional components that are easily testable. The project now has 100% code coverage. |
| 132 |
It utilizes tools like [Hypothesis](https://hypothesis.readthedocs.io/en/latest/) to reduce the number of unexpected errors. |
| 133 |
It went from fully dynamic to fully static typing using mypy. Finally, it utilizes the latest linters both on (like [DeepSource](https://deepsource.io/gh/pycqa/isort/)) and offline (like [Flake8](https://flake8.pycqa.org/en/latest/)) to help ensure a higher bar for all code contributions into the future. |
| 134 |
|
| 135 |
## Give 5.0.0 a try! |
| 136 |
|
| 137 |
[Try isort 5 right now from your browser!](../quick_start/0.-try.md) |
| 138 |
|
| 139 |
OR |
| 140 |
|
| 141 |
Install isort locally using `pip3 install isort`. |
| 142 |
|
| 143 |
[Click here for full installation instructions.](../quick_start/1.-install.md) |