| 1 |
[](https://isort.readthedocs.io/) |
| 2 |
|
| 3 |
------------------------------------------------------------------------ |
| 4 |
|
| 5 |
[](https://badge.fury.io/py/isort) |
| 6 |
[][pypi status] |
| 7 |
[](https://github.com/PyCQA/isort/actions/workflows/continuous-integration.yml) |
| 8 |
[](https://codecov.io/gh/pycqa/isort) |
| 9 |
[](https://pypi.org/project/isort/) |
| 10 |
[](https://pepy.tech/project/isort) |
| 11 |
[](https://github.com/psf/black) |
| 12 |
[](https://isort.readthedocs.io/) |
| 13 |
[](https://deepsource.io/gh/pycqa/isort/?ref=repository-badge) |
| 14 |
|
| 15 |
[pypi status]: https://pypi.org/project/isort/ |
| 16 |
_________________ |
| 17 |
|
| 18 |
[Read Latest Documentation](https://isort.readthedocs.io/) - [Browse GitHub Code Repository](https://github.com/pycqa/isort/) |
| 19 |
_________________ |
| 20 |
|
| 21 |
# isort your imports, so you don't have to. |
| 22 |
|
| 23 |
isort is a Python utility / library to sort imports alphabetically and |
| 24 |
automatically separate into sections and by type. It provides a command line |
| 25 |
utility, Python library and [plugins for various |
| 26 |
editors](https://github.com/pycqa/isort/wiki/isort-Plugins) to |
| 27 |
quickly sort all your imports. It requires Python 3.10+ to run but |
| 28 |
supports formatting Python 2 code too. |
| 29 |
|
| 30 |
- [Try isort now from your browser!](docs/quick_start/0.-try.md) |
| 31 |
- [Using black? See the isort and black compatibility guide.](docs/configuration/black_compatibility.md) |
| 32 |
- [isort has official support for pre-commit!](docs/configuration/pre-commit.md) |
| 33 |
|
| 34 |
 |
| 35 |
|
| 36 |
Before isort: |
| 37 |
|
| 38 |
```python |
| 39 |
from my_lib import Object |
| 40 |
|
| 41 |
import os |
| 42 |
|
| 43 |
from my_lib import Object3 |
| 44 |
|
| 45 |
from my_lib import Object2 |
| 46 |
|
| 47 |
import sys |
| 48 |
|
| 49 |
from third_party import lib15, lib1, lib2, lib3, lib4, lib5, lib6, lib7, lib8, lib9, lib10, lib11, lib12, lib13, lib14 |
| 50 |
|
| 51 |
import sys |
| 52 |
|
| 53 |
from __future__ import absolute_import |
| 54 |
|
| 55 |
from third_party import lib3 |
| 56 |
|
| 57 |
print("Hey") |
| 58 |
print("yo") |
| 59 |
``` |
| 60 |
|
| 61 |
After isort: |
| 62 |
|
| 63 |
```python |
| 64 |
from __future__ import absolute_import |
| 65 |
|
| 66 |
import os |
| 67 |
import sys |
| 68 |
|
| 69 |
from third_party import (lib1, lib2, lib3, lib4, lib5, lib6, lib7, lib8, |
| 70 |
lib9, lib10, lib11, lib12, lib13, lib14, lib15) |
| 71 |
|
| 72 |
from my_lib import Object, Object2, Object3 |
| 73 |
|
| 74 |
print("Hey") |
| 75 |
print("yo") |
| 76 |
``` |
| 77 |
|
| 78 |
## Installing isort |
| 79 |
|
| 80 |
Installing isort is as simple as: |
| 81 |
|
| 82 |
```bash |
| 83 |
pip install isort |
| 84 |
``` |
| 85 |
|
| 86 |
## Using isort |
| 87 |
|
| 88 |
**From the command line**: |
| 89 |
|
| 90 |
To run on specific files: |
| 91 |
|
| 92 |
```bash |
| 93 |
isort mypythonfile.py mypythonfile2.py |
| 94 |
``` |
| 95 |
|
| 96 |
To apply recursively: |
| 97 |
|
| 98 |
```bash |
| 99 |
isort . |
| 100 |
``` |
| 101 |
|
| 102 |
If [globstar](https://www.gnu.org/software/bash/manual/html_node/The-Shopt-Builtin.html) |
| 103 |
is enabled, `isort .` is equivalent to: |
| 104 |
|
| 105 |
```bash |
| 106 |
isort **/*.py |
| 107 |
``` |
| 108 |
|
| 109 |
To view proposed changes without applying them: |
| 110 |
|
| 111 |
```bash |
| 112 |
isort mypythonfile.py --diff |
| 113 |
``` |
| 114 |
|
| 115 |
Finally, to atomically run isort against a project, only applying |
| 116 |
changes if they don't introduce syntax errors: |
| 117 |
|
| 118 |
```bash |
| 119 |
isort --atomic . |
| 120 |
``` |
| 121 |
|
| 122 |
(Note: this is disabled by default, as it prevents isort from |
| 123 |
running against code written using a different version of Python.) |
| 124 |
|
| 125 |
**From within Python**: |
| 126 |
|
| 127 |
```python |
| 128 |
import isort |
| 129 |
|
| 130 |
isort.file("pythonfile.py") |
| 131 |
``` |
| 132 |
|
| 133 |
or: |
| 134 |
|
| 135 |
```python |
| 136 |
import isort |
| 137 |
|
| 138 |
sorted_code = isort.code("import b\nimport a\n") |
| 139 |
``` |
| 140 |
|
| 141 |
## Installing isort's for your preferred text editor |
| 142 |
|
| 143 |
Several plugins have been written that enable to use isort from within a |
| 144 |
variety of text-editors. You can find a full list of them [on the isort |
| 145 |
wiki](https://github.com/pycqa/isort/wiki/isort-Plugins). |
| 146 |
Additionally, I will enthusiastically accept pull requests that include |
| 147 |
plugins for other text editors and add documentation for them as I am |
| 148 |
notified. |
| 149 |
|
| 150 |
## Multi line output modes |
| 151 |
|
| 152 |
You will notice above the \"multi\_line\_output\" setting. This setting |
| 153 |
defines how from imports wrap when they extend past the line\_length |
| 154 |
limit and has [12 possible settings](docs/configuration/multi_line_output_modes.md). |
| 155 |
|
| 156 |
## Indentation |
| 157 |
|
| 158 |
To change the how constant indents appear - simply change the |
| 159 |
indent property with the following accepted formats: |
| 160 |
|
| 161 |
- Number of spaces you would like. For example: 4 would cause standard |
| 162 |
4 space indentation. |
| 163 |
- Tab |
| 164 |
- A verbatim string with quotes around it. |
| 165 |
|
| 166 |
For example: |
| 167 |
|
| 168 |
```python |
| 169 |
" " |
| 170 |
``` |
| 171 |
|
| 172 |
is equivalent to 4. |
| 173 |
|
| 174 |
For the import styles that use parentheses, you can control whether or |
| 175 |
not to include a trailing comma after the last import with the |
| 176 |
`include_trailing_comma` option (defaults to `False`). |
| 177 |
|
| 178 |
## Intelligently Balanced Multi-line Imports |
| 179 |
|
| 180 |
As of isort 3.1.0 support for balanced multi-line imports has been |
| 181 |
added. With this enabled isort will dynamically change the import length |
| 182 |
to the one that produces the most balanced grid, while staying below the |
| 183 |
maximum import length defined. |
| 184 |
|
| 185 |
Example: |
| 186 |
|
| 187 |
```python |
| 188 |
from __future__ import (absolute_import, division, |
| 189 |
print_function, unicode_literals) |
| 190 |
``` |
| 191 |
|
| 192 |
Will be produced instead of: |
| 193 |
|
| 194 |
```python |
| 195 |
from __future__ import (absolute_import, division, print_function, |
| 196 |
unicode_literals) |
| 197 |
``` |
| 198 |
|
| 199 |
To enable this set `balanced_wrapping` to `True` in your config or pass |
| 200 |
the `-e` option into the command line utility. |
| 201 |
|
| 202 |
## Custom Sections and Ordering |
| 203 |
|
| 204 |
isort provides configuration options to change almost every aspect of how |
| 205 |
imports are organized, ordered, or grouped together in sections. |
| 206 |
|
| 207 |
[Click here](docs/configuration/custom_sections_and_ordering.md) for an overview of all these options. |
| 208 |
|
| 209 |
## Skip processing of imports (outside of configuration) |
| 210 |
|
| 211 |
To make isort ignore a single import simply add a comment at the end of |
| 212 |
the import line containing the text `isort:skip`: |
| 213 |
|
| 214 |
```python |
| 215 |
import module # isort:skip |
| 216 |
``` |
| 217 |
|
| 218 |
or: |
| 219 |
|
| 220 |
```python |
| 221 |
from xyz import (abc, # isort:skip |
| 222 |
yo, |
| 223 |
hey) |
| 224 |
``` |
| 225 |
|
| 226 |
To make isort skip an entire file simply add `isort:skip_file` to the |
| 227 |
module's doc string: |
| 228 |
|
| 229 |
```python |
| 230 |
""" my_module.py |
| 231 |
Best module ever |
| 232 |
|
| 233 |
isort:skip_file |
| 234 |
""" |
| 235 |
|
| 236 |
import b |
| 237 |
import a |
| 238 |
``` |
| 239 |
|
| 240 |
## Adding or removing an import from multiple files |
| 241 |
|
| 242 |
isort can be ran or configured to add / remove imports automatically. |
| 243 |
|
| 244 |
[See a complete guide here.](docs/configuration/add_or_remove_imports.md) |
| 245 |
|
| 246 |
## Using isort to verify code |
| 247 |
|
| 248 |
The `--check-only` option |
| 249 |
------------------------- |
| 250 |
|
| 251 |
isort can also be used to verify that code is correctly formatted |
| 252 |
by running it with `-c`. Any files that contain incorrectly sorted |
| 253 |
and/or formatted imports will be outputted to `stderr`. |
| 254 |
|
| 255 |
```bash |
| 256 |
isort **/*.py -c -v |
| 257 |
|
| 258 |
SUCCESS: /home/timothy/Projects/Open_Source/isort/isort_kate_plugin.py Everything Looks Good! |
| 259 |
ERROR: /home/timothy/Projects/Open_Source/isort/isort/isort.py Imports are incorrectly sorted. |
| 260 |
``` |
| 261 |
|
| 262 |
One great place this can be used is with a pre-commit git hook, such as |
| 263 |
this one by \@acdha: |
| 264 |
|
| 265 |
<https://gist.github.com/acdha/8717683> |
| 266 |
|
| 267 |
This can help to ensure a certain level of code quality throughout a |
| 268 |
project. |
| 269 |
|
| 270 |
## Git hook |
| 271 |
|
| 272 |
isort provides a hook function that can be integrated into your Git |
| 273 |
pre-commit script to check Python code before committing. |
| 274 |
|
| 275 |
[More info here.](docs/configuration/git_hook.md) |
| 276 |
|
| 277 |
## Spread the word |
| 278 |
|
| 279 |
[](https://isort.readthedocs.io/) |
| 280 |
|
| 281 |
Place this badge at the top of your repository to let others know your project uses isort. |
| 282 |
|
| 283 |
For README.md: |
| 284 |
|
| 285 |
```markdown |
| 286 |
[](https://isort.readthedocs.io/) |
| 287 |
``` |
| 288 |
|
| 289 |
Or README.rst: |
| 290 |
|
| 291 |
```rst |
| 292 |
.. image:: https://img.shields.io/badge/imports-isort-%231674b1?style=flat&labelColor=ef8336 |
| 293 |
:target: https://isort.readthedocs.io/ |
| 294 |
``` |
| 295 |
|
| 296 |
## Security contact information |
| 297 |
|
| 298 |
To report a security vulnerability, please use the [Tidelift security |
| 299 |
contact](https://tidelift.com/security). Tidelift will coordinate the |
| 300 |
fix and disclosure. |
| 301 |
|
| 302 |
## Why isort? |
| 303 |
|
| 304 |
isort simply stands for import sort. It was originally called |
| 305 |
"sortImports" however I got tired of typing the extra characters and |
| 306 |
came to the realization camelCase is not pythonic. |
| 307 |
|
| 308 |
I wrote isort because in an organization I used to work in the manager |
| 309 |
came in one day and decided all code must have alphabetically sorted |
| 310 |
imports. The code base was huge - and he meant for us to do it by hand. |
| 311 |
However, being a programmer - I\'m too lazy to spend 8 hours mindlessly |
| 312 |
performing a function, but not too lazy to spend 16 hours automating it. |
| 313 |
I was given permission to open source sortImports and here we are :) |
| 314 |
|
| 315 |
------------------------------------------------------------------------ |
| 316 |
|
| 317 |
[Get professionally supported isort with the Tidelift |
| 318 |
Subscription](https://tidelift.com/subscription/pkg/pypi-isort?utm_source=pypi-isort&utm_medium=referral&utm_campaign=readme) |
| 319 |
|
| 320 |
Professional support for isort is available as part of the [Tidelift |
| 321 |
Subscription](https://tidelift.com/subscription/pkg/pypi-isort?utm_source=pypi-isort&utm_medium=referral&utm_campaign=readme). |
| 322 |
Tidelift gives software development teams a single source for purchasing |
| 323 |
and maintaining their software, with professional grade assurances from |
| 324 |
the experts who know it best, while seamlessly integrating with existing |
| 325 |
tools. |
| 326 |
|
| 327 |
------------------------------------------------------------------------ |
| 328 |
|
| 329 |
Thanks and I hope you find isort useful! |
| 330 |
|
| 331 |
~Timothy Crosley |