Maintaining consistent formatting across your project is a simple yet effective way to improve the readability of your code. To achieve this, I use two complementary formatters: isort for sorting imports and black for the rest of the code.
black is a code formatter that aims to be the standard formatting tool for Python. It has many advantages:
- Ease of use.
- PEP 8 compliance (of course).
- Minimalistic configuration, which ensures that the formatting stays consistent across different developers.
- Deterministic (it always produces the same output), so it can be used on CI/CD pipelines.
# format all the Python files of your project
black .
isort is a tool that formats a part of your code left untouched by black: the order of your import statements. It splits your import list into 3 parts, as recommended by PEP-8: first the standard library, then the third-party modules, and finally your own package.
# order all the imports of your Python files
isort .
Add the following configuration to your pyproject.toml
.
If you don't use git, turn skip_gitignore
to false
.
[tool.isort]
profile = "black"
skip_gitignore = true