| 1 |
[](https://black.readthedocs.io/en/stable/) |
| 2 |
|
| 3 |
<h2 align="center">The Uncompromising Code Formatter</h2> |
| 4 |
|
| 5 |
<p align="center"> |
| •6 |
<a href="https://github.com/psf/black/actions">Actions Status</a> |
| 7 |
<a href="https://black.readthedocs.io/en/stable/?badge=stable">Documentation Status</a> |
| 8 |
<a href="https://coveralls.io/github/psf/black?branch=main">Coverage Status</a> |
| 9 |
<a href="https://github.com/psf/black/blob/main/LICENSE">License: MIT</a> |
| 10 |
<a href="https://pypi.org/project/black/">PyPI</a> |
| 11 |
<a href="https://pypi.org/project/black">Supported Python Versions</a> |
| 12 |
<a href="https://pepy.tech/project/black">Downloads</a> |
| 13 |
<a href="https://anaconda.org/conda-forge/black/">conda-forge</a> |
| 14 |
<a href="https://github.com/psf/black">Code style: black</a> |
| 15 |
</p> |
| 16 |
|
| 17 |
> “Any color you like.” |
| 18 |
|
| 19 |
_Black_ is the uncompromising Python code formatter. By using it, you agree to cede |
| 20 |
control over minutiae of hand-formatting. In return, _Black_ gives you speed, |
| 21 |
determinism, and freedom from `pycodestyle` nagging about formatting. You will save time |
| 22 |
and mental energy for more important matters. |
| 23 |
|
| 24 |
Blackened code looks the same regardless of the project you're reading. Formatting |
| 25 |
becomes transparent after a while and you can focus on the content instead. |
| 26 |
|
| 27 |
_Black_ makes code review faster by producing the smallest diffs possible. |
| 28 |
|
| 29 |
Watch the [PyCon 2019 talk](https://youtu.be/esZLCuWs_2Y) to learn more. |
| 30 |
|
| 31 |
--- |
| 32 |
|
| 33 |
**[Read the documentation on ReadTheDocs!](https://black.readthedocs.io/en/stable)** |
| 34 |
|
| 35 |
--- |
| 36 |
|
| 37 |
## Installation and usage |
| 38 |
|
| 39 |
### Installation |
| 40 |
|
| 41 |
_Black_ can be installed by running `pip install black`. It requires Python 3.10+ to |
| 42 |
run. If you want to format Jupyter Notebooks, install with |
| 43 |
`pip install "black[jupyter]"`. |
| 44 |
|
| 45 |
### Usage |
| 46 |
|
| 47 |
To get started right away with sensible defaults: |
| 48 |
|
| 49 |
```sh |
| 50 |
black {source_file_or_directory} |
| 51 |
``` |
| 52 |
|
| 53 |
You can run _Black_ as a package if running it as a script doesn't work: |
| 54 |
|
| 55 |
```sh |
| 56 |
python -m black {source_file_or_directory} |
| 57 |
``` |
| 58 |
|
| 59 |
Further information can be found in our docs: |
| 60 |
|
| 61 |
- [Usage and Configuration](https://black.readthedocs.io/en/stable/usage_and_configuration/index.html) |
| 62 |
|
| 63 |
_Black_ is already [successfully used](https://github.com/psf/black#used-by) by many |
| 64 |
projects, small and big. _Black_ has a comprehensive test suite, with efficient parallel |
| 65 |
tests, and our own auto formatting and parallel Continuous Integration runner. Now that |
| 66 |
we have become stable, you should not expect large formatting changes in the future. |
| 67 |
Stylistic changes will mostly be responses to bug reports and support for new Python |
| 68 |
syntax. For more information please refer to |
| 69 |
[The Black Code Style](https://black.readthedocs.io/en/stable/the_black_code_style/index.html). |
| 70 |
|
| 71 |
Also, as a safety measure which slows down processing, _Black_ will check that the |
| 72 |
reformatted code still produces a valid AST that is effectively equivalent to the |
| 73 |
original (see the |
| 74 |
[Pragmatism](https://black.readthedocs.io/en/stable/the_black_code_style/current_style.html#ast-before-and-after-formatting) |
| 75 |
section for details). If you're feeling confident, use `--fast`. |
| 76 |
|
| 77 |
## The _Black_ code style |
| 78 |
|
| 79 |
_Black_ is a PEP 8 compliant opinionated formatter. _Black_ reformats entire files in |
| 80 |
place. Style configuration options are deliberately limited and rarely added. It doesn't |
| 81 |
take previous formatting into account (see |
| 82 |
[Pragmatism](https://black.readthedocs.io/en/stable/the_black_code_style/current_style.html#pragmatism) |
| 83 |
for exceptions). |
| 84 |
|
| 85 |
Our documentation covers the current _Black_ code style, but planned changes to it are |
| 86 |
also documented. They're both worth taking a look at: |
| 87 |
|
| 88 |
- [The _Black_ Code Style: Current style](https://black.readthedocs.io/en/stable/the_black_code_style/current_style.html) |
| 89 |
- [The _Black_ Code Style: Future style](https://black.readthedocs.io/en/stable/the_black_code_style/future_style.html) |
| 90 |
|
| 91 |
Changes to the _Black_ code style are bound by the Stability Policy: |
| 92 |
|
| 93 |
- [The _Black_ Code Style: Stability Policy](https://black.readthedocs.io/en/stable/the_black_code_style/index.html#stability-policy) |
| 94 |
|
| 95 |
Please refer to this document before submitting an issue. What seems like a bug might be |
| 96 |
intended behaviour. |
| 97 |
|
| 98 |
### Pragmatism |
| 99 |
|
| 100 |
Early versions of _Black_ used to be absolutist in some respects. They took after its |
| 101 |
initial author. This was fine at the time as it made the implementation simpler and |
| 102 |
there were not many users anyway. Not many edge cases were reported. As a mature tool, |
| 103 |
_Black_ does make some exceptions to rules it otherwise holds. |
| 104 |
|
| 105 |
- [The _Black_ code style: Pragmatism](https://black.readthedocs.io/en/stable/the_black_code_style/current_style.html#pragmatism) |
| 106 |
|
| 107 |
Please refer to this document before submitting an issue just like with the document |
| 108 |
above. What seems like a bug might be intended behaviour. |
| 109 |
|
| 110 |
## Configuration |
| 111 |
|
| 112 |
_Black_ is able to read project-specific default values for its command line options |
| 113 |
from a `pyproject.toml` file. This is especially useful for specifying custom |
| 114 |
`--include` and `--exclude`/`--force-exclude`/`--extend-exclude` patterns for your |
| 115 |
project. |
| 116 |
|
| 117 |
You can find more details in our documentation: |
| 118 |
|
| 119 |
- [The basics: Configuration via a file](https://black.readthedocs.io/en/stable/usage_and_configuration/the_basics.html#configuration-via-a-file) |
| 120 |
|
| 121 |
And if you're looking for more general configuration documentation: |
| 122 |
|
| 123 |
- [Usage and Configuration](https://black.readthedocs.io/en/stable/usage_and_configuration/index.html) |
| 124 |
|
| 125 |
**Pro-tip**: If you're asking yourself "Do I need to configure anything?" the answer is |
| 126 |
"No". _Black_ is all about sensible defaults. Applying those defaults will have your |
| 127 |
code in compliance with many other _Black_ formatted projects. |
| 128 |
|
| 129 |
## Used by |
| 130 |
|
| 131 |
The following notable open-source projects trust _Black_ with enforcing a consistent |
| 132 |
code style: pytest, tox, Pyramid, Django, Django Channels, Hypothesis, attrs, |
| 133 |
SQLAlchemy, Poetry, PyPA applications (Warehouse, Bandersnatch, Pipenv, virtualenv), |
| 134 |
pandas, Pillow, Twisted, LocalStack, every Datadog Agent Integration, Home Assistant, |
| 135 |
Zulip, Kedro, OpenOA, FLORIS, ORBIT, WOMBAT, and many more. |
| 136 |
|
| 137 |
The following organizations use _Black_: Dropbox, KeepTruckin, Lyft, Mozilla, Quora, |
| 138 |
Duolingo, QuantumBlack, Tesla, Archer Aviation. |
| 139 |
|
| 140 |
Are we missing anyone? Let us know. |
| 141 |
|
| 142 |
## Testimonials |
| 143 |
|
| 144 |
**Mike Bayer**, [author of `SQLAlchemy`](https://www.sqlalchemy.org/): |
| 145 |
|
| 146 |
> I can't think of any single tool in my entire programming career that has given me a |
| 147 |
> bigger productivity increase by its introduction. I can now do refactorings in about |
| 148 |
> 1% of the keystrokes that it would have taken me previously when we had no way for |
| 149 |
> code to format itself. |
| 150 |
|
| 151 |
**Dusty Phillips**, |
| 152 |
[writer](https://smile.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Daps&field-keywords=dusty+phillips): |
| 153 |
|
| 154 |
> _Black_ is opinionated so you don't have to be. |
| 155 |
|
| 156 |
**Hynek Schlawack**, [creator of `attrs`](https://www.attrs.org/), core developer of |
| 157 |
Twisted and CPython: |
| 158 |
|
| 159 |
> An auto-formatter that doesn't suck is all I want for Xmas! |
| 160 |
|
| 161 |
**Carl Meyer**, [Django](https://www.djangoproject.com/) core developer: |
| 162 |
|
| 163 |
> At least the name is good. |
| 164 |
|
| 165 |
**Kenneth Reitz**, creator of [`requests`](https://requests.readthedocs.io/en/latest/) |
| 166 |
and [`pipenv`](https://readthedocs.org/projects/pipenv/): |
| 167 |
|
| 168 |
> This vastly improves the formatting of our code. Thanks a ton! |
| 169 |
|
| 170 |
## Show your style |
| 171 |
|
| 172 |
Use the badge in your project's README.md: |
| 173 |
|
| 174 |
```md |
| 175 |
[](https://github.com/psf/black) |
| 176 |
``` |
| 177 |
|
| 178 |
Using the badge in README.rst: |
| 179 |
|
| 180 |
```rst |
| 181 |
.. image:: https://img.shields.io/badge/code%20style-black-000000.svg |
| 182 |
:target: https://github.com/psf/black |
| 183 |
``` |
| 184 |
|
| 185 |
## License |
| 186 |
|
| 187 |
MIT |
| 188 |
|
| 189 |
## Contributing |
| 190 |
|
| 191 |
Welcome! Happy to see you willing to make the project better. You can get started by |
| 192 |
reading this: |
| 193 |
|
| 194 |
- [Contributing: The basics](https://black.readthedocs.io/en/latest/contributing/the_basics.html) |
| 195 |
|
| 196 |
You can also take a look at the rest of the contributing docs or talk with the |
| 197 |
developers: |
| 198 |
|
| 199 |
- [Contributing documentation](https://black.readthedocs.io/en/latest/contributing/index.html) |
| 200 |
- [Chat on Discord](https://discord.gg/RtVdv86PrH) |
| 201 |
|
| 202 |
## Change log |
| 203 |
|
| 204 |
The log has become rather long. It moved to its own file. |
| 205 |
|
| 206 |
See [CHANGES](https://black.readthedocs.io/en/latest/change_log.html). |
| 207 |
|
| 208 |
## Authors |
| 209 |
|
| 210 |
The author list is quite long nowadays, so it lives in its own file. |
| 211 |
|
| 212 |
See [AUTHORS.md](./AUTHORS.md) |
| 213 |
|
| 214 |
## Code of Conduct |
| 215 |
|
| 216 |
Everyone participating in the _Black_ project, and in particular in the issue tracker, |
| 217 |
pull requests, and social media activity, is expected to treat other people with respect |
| 218 |
and more generally to follow the guidelines articulated in the |
| 219 |
[Python Community Code of Conduct](https://www.python.org/psf/codeofconduct/). |
| 220 |
|
| 221 |
At the same time, humor is encouraged. In fact, basic familiarity with Monty Python's |
| 222 |
Flying Circus is expected. We are not savages. |
| 223 |
|
| 224 |
And if you _really_ need to slap somebody, do it with a fish while dancing. |