Debian Codegraph @

docs/configuration/config_files.md

raw
1 Supported config files
2 ========
3
4 isort supports various standard config formats to allow customizations to be integrated into any project quickly.
5 When applying configurations, isort looks for the closest supported config file, in the order files are listed below.
6 You can manually specify the settings file or path by setting `--settings-path` from the command-line. Otherwise, isort will
7 traverse up to 25 parent directories until it finds a suitable config file.
8 Note that isort will not leave a git or Mercurial repository (checking for a `.git` file or directory,
9 or a `.hg` directory).
10 As soon as it finds a file, it stops looking. The config file search is done relative to the current directory if `isort .`
11 or a file stream is passed in, or relative to the first path passed in if multiple paths are passed in.
12 isort **never** merges config files together due to the confusion it can cause.
13
14 !!! tip
15 You can always introspect the configuration settings isort determined, and find out which config file it picked up, by running `isort . --show-config`
16
17
18
19 ## .isort.cfg **[preferred format]**
20
21 The first place isort will look for settings is in dedicated .isort.cfg files.
22 The advantage of using this kind of config file, is that it is explicitly for isort and follows a well understood format.
23 The downside, is that it means one more config file in your project when you may already have several polluting your file hierarchy.
24
25 An example a config from the isort project itself:
26
27 ```ini
28 [settings]
29 profile=hug
30 src_paths=isort,test
31 ```
32
33 ## pyproject.toml **[preferred format]**
34
35 The second place isort will look, and an equally excellent choice to place your configuration, is within a pyproject.toml file.
36 The advantage of using this config file, is that it is quickly becoming a standard place to configure all Python tools.
37 This means other developers will know to look here and you will keep your projects root nice and tidy.
38 The only disadvantage is that other tools you use might not yet support this format, negating the cleanliness.
39
40 ```toml
41 [tool.isort]
42 profile = "hug"
43 src_paths = ["isort", "test"]
44 ```
45
46 ## setup.cfg
47
48 `setup.cfg` can be thought of as the precursor to `pyproject.toml`. While isort and newer tools are increasingly moving to pyproject.toml, if you rely on many tools that
49 use this standard it can be a natural fit to put your isort config there as well.
50
51
52 ```ini
53 [isort]
54 profile=hug
55 src_paths=isort,test
56 ```
57
58 ## tox.ini
59
60 [tox](https://tox.readthedocs.io/en/latest/) is a tool commonly used in the Python community to specify multiple testing environments.
61 Because isort verification is commonly ran as a testing step, some prefer to place the isort config inside of the tox.ini file.
62
63 ```ini
64 [isort]
65 profile = black
66 multi_line_output = 3
67 ```
68
69 ## .editorconfig
70
71 Finally, isort will look for a `.editorconfig` configuration with settings for Python source files. [EditorConfig](https://editorconfig.org/) is a project to enable specifying a configuration for text editing behaviour once, allowing multiple command line tools and text editors to pick it up. Since isort cares about a lot of the same settings as a text-editor (like line-length) it makes sense for it to look within these files
72 as well.
73
74 ```
75 root = true
76
77 [*.py]
78 profile = hug
79 indent_style = space
80 indent_size = 4
81 skip = build,.tox,venv
82 src_paths=isort,test
83 ```
84
85 ## Custom config files
86
87 Optionally, you can also create a config file with a custom name, or directly point isort to a config file that falls lower in the priority order, by using [--settings-file](./options.md#settings-path).
88 This can be useful, for instance, if you want to have one configuration for `.py` files and another for `.pyx` - while keeping the config files at the root of your repository.
89
90 !!! tip
91 Custom config files should place their configuration options inside an `[isort]` section and never a generic `[settings]` section. This is because isort can't know for sure
92 how other tools are utilizing the config file.
93
94
95 ## Supporting multiple config files in single isort run
96
97 If you have a directory structure where different sub-directories may have their separate configuration settings and you want isort to respect these configurations, not just apply the same global configuration for the entire directory then you can do so with the `--resolve-all-configs` flag. Using the `--resolve-all-configs` along with providing the directory root as `--config-root` argument(if the config-root is not explicitly defined, then isort will consider the current directory `.` where the shell is running), isort will traverse and parse all the config files defined under the `--config-root` and dynamically decide what configurations should be applied to a specific file by choosing the nearest config file in the file's path. For instance, if your directory structure is
98
99 ```
100 directory_root
101
102 subdir1
103 .isort.cfg
104 file1.py
105
106 subdir2
107 pyproject.toml
108 file2.py
109
110 subdir3
111 file3.py
112
113 setup.cfg
114 ```
115
116 isort will sort `subdir1/file1` according to the configurations defined in `subdir1/.isort.cfg`, `subdir2/file2` with configurations from `subdir2/pyproject.toml` and `subdir3/file3.py` based on the `setup.cfg` settings.
117
118 !!! tip
119 You can always confirm exactly what config file was used for a file by running isort with the `--verbose` flag.