| 1 |
# Custom sections and ordering |
| 2 |
|
| 3 |
isort provides lots of features to enable configuring how it sections imports |
| 4 |
and how it sorts imports within those sections. |
| 5 |
You can change the section order with `sections` option from the default |
| 6 |
of: |
| 7 |
|
| 8 |
```ini |
| 9 |
FUTURE,STDLIB,THIRDPARTY,FIRSTPARTY,LOCALFOLDER |
| 10 |
``` |
| 11 |
|
| 12 |
to your preference (if defined, omitting a default section may cause errors): |
| 13 |
|
| 14 |
```ini |
| 15 |
sections=FUTURE,STDLIB,FIRSTPARTY,THIRDPARTY,LOCALFOLDER |
| 16 |
``` |
| 17 |
|
| 18 |
You also can define your own sections and their order. |
| 19 |
|
| 20 |
Example: |
| 21 |
|
| 22 |
```ini |
| 23 |
known_django=django |
| 24 |
known_pandas=pandas,numpy |
| 25 |
sections=FUTURE,STDLIB,DJANGO,THIRDPARTY,PANDAS,FIRSTPARTY,LOCALFOLDER |
| 26 |
``` |
| 27 |
|
| 28 |
would create two new sections with the specified known modules. |
| 29 |
|
| 30 |
The `no_lines_before` option will prevent the listed sections from being |
| 31 |
split from the previous section by an empty line. |
| 32 |
|
| 33 |
Example: |
| 34 |
|
| 35 |
```ini |
| 36 |
sections=FUTURE,STDLIB,THIRDPARTY,FIRSTPARTY,LOCALFOLDER |
| 37 |
no_lines_before=LOCALFOLDER |
| 38 |
``` |
| 39 |
|
| 40 |
would produce a section with both FIRSTPARTY and LOCALFOLDER modules |
| 41 |
combined. |
| 42 |
|
| 43 |
**IMPORTANT NOTE**: It is very important to know when setting `known` sections that the naming |
| 44 |
does not directly map for historical reasons. For custom settings, the only difference is |
| 45 |
capitalization (`known_custom=custom` VS `sections=CUSTOM,...`) for all others reference the |
| 46 |
following mapping: |
| 47 |
|
| 48 |
- `known_standard_library` : `STANDARD_LIBRARY` |
| 49 |
- `extra_standard_library` : `STANDARD_LIBRARY` # Like known standard library but appends instead of replacing |
| 50 |
- `known_future_library` : `FUTURE` |
| 51 |
- `known_first_party`: `FIRSTPARTY` |
| 52 |
- `known_third_party`: `THIRDPARTY` |
| 53 |
- `known_local_folder`: `LOCALFOLDER` |
| 54 |
|
| 55 |
This will likely be changed in isort 6.0.0+ in a backwards compatible way. |
| 56 |
|
| 57 |
|
| 58 |
## Auto-comment import sections |
| 59 |
|
| 60 |
Some projects prefer to have import sections uniquely titled to aid in |
| 61 |
identifying the sections quickly when visually scanning. isort can |
| 62 |
automate this as well. To do this simply set the |
| 63 |
`import_heading_{section_name}` setting for each section you wish to |
| 64 |
have auto commented - to the desired comment. |
| 65 |
|
| 66 |
For Example: |
| 67 |
|
| 68 |
```ini |
| 69 |
import_heading_stdlib=Standard Library |
| 70 |
import_heading_firstparty=My Stuff |
| 71 |
``` |
| 72 |
|
| 73 |
Would lead to output looking like the following: |
| 74 |
|
| 75 |
```python |
| 76 |
# Standard Library |
| 77 |
import os |
| 78 |
import sys |
| 79 |
|
| 80 |
import django.settings |
| 81 |
|
| 82 |
# My Stuff |
| 83 |
import myproject.test |
| 84 |
``` |
| 85 |
|
| 86 |
## Ordering by import length |
| 87 |
|
| 88 |
isort also makes it easy to sort your imports by length, simply by |
| 89 |
setting the `length_sort` option to `True`. This will result in the |
| 90 |
following output style: |
| 91 |
|
| 92 |
```python |
| 93 |
from evn.util import ( |
| 94 |
Pool, |
| 95 |
Dict, |
| 96 |
Options, |
| 97 |
Constant, |
| 98 |
DecayDict, |
| 99 |
UnexpectedCodePath, |
| 100 |
) |
| 101 |
``` |
| 102 |
|
| 103 |
It is also possible to opt-in to sorting imports by length for only |
| 104 |
specific sections by using `length_sort_` followed by the section name |
| 105 |
as a configuration item, e.g.: |
| 106 |
|
| 107 |
length_sort_stdlib=1 |
| 108 |
|
| 109 |
## Controlling how isort sections `from` imports |
| 110 |
|
| 111 |
By default isort places straight (`import y`) imports above from imports (`from x import y`): |
| 112 |
|
| 113 |
```python |
| 114 |
import b |
| 115 |
from a import a # This will always appear below because it is a from import. |
| 116 |
``` |
| 117 |
|
| 118 |
However, if you prefer to keep strict alphabetical sorting you can set [force sort within sections](./options.md#force-sort-within-sections) to true. Resulting in: |
| 119 |
|
| 120 |
|
| 121 |
```python |
| 122 |
from a import a # This will now appear at top because a appears in the alphabet before b |
| 123 |
import b |
| 124 |
``` |
| 125 |
|
| 126 |
You can even tell isort to always place from imports on top, instead of the default of placing them on bottom, using [from first](./options.md#from-first). |
| 127 |
|
| 128 |
```python |
| 129 |
from b import b # If from first turnrf on, all from imports are placed before non-from imports. |
| 130 |
import a |
| 131 |
``` |