Python Code Formatters
Look at the most popular code formatters in Python and help you take a decision on which one you should adopt.
Join the DZone community and get the full member experience.Join For Free
If you write code that other developers review or contribute to, chances are you’re already adhering to a style guide for writing code. In this post, we look at the most popular code formatters in Python and help you decide on which one you should adopt.
The most popular code formatters in Python is:
Why Use Code Formatters for Python?
Following a style guide keeps the code’s aesthetics clean and improves readability, making contributions and code reviews easier. Automated code formatters ensure your codebase stays in a consistent style without any manual work on your end. If adhering to a specific coding style is important to you, employing an automated to do that job is the obvious thing to do. This avoids bike-shedding on nitpicks during code reviews, saving you an enormous amount of time overall.
Black is used by some trendy open-source projects, such as pytest, tox, Pyramid, Django Channels, Poetry, and so on.
How to use Black?
Install the latest version of Black from PyPI:
pip install black
And then, run black on one or many files. On doing so, Black will format the files in-place.
PEP 8, Python’s official style guide, recommends the following rules for writing import statements:
Imports are always put at the top of the file, just after any module comments and docstrings, and before module globals and constants.
Imports should be grouped in the following order:
- Standard library imports.
- Related third party imports.
- Local application/library specific imports.
You should put a blank line between each group of imports.
Isort sorts imports in your Python files automatically — alphabetically, separated into sections, and by type. It ensures that the imports in a Python file follow the recommended PEP 8 guidelines.
How to Use Isort?
Install the latest version of isort from PyPI:
pip install isort
And then, run isort on one or many files. On doing so, isort will format the files in-place.
isort . # runs recursively on the current working directory
autopep8 is an unofficial yet popular tool that automatically formates Python code to conform to PEP 8. It uses pycodestyle, Python’s official PEP 8 violation checker tool, to determine what parts of the code need to be formatted.
How to Use Autopep8?
Install the latest version of autopep8 from PyPI:
pip install autopep8
And then, run autopep8 on one or many files. On doing so, autopep8 will format the files in-place.
autopep8 --in-place <filename>
YAPF, or Yet Another Python Formatter, takes a different formatting code approach than the other tools listed here. It works on the premise that code that conforms to the PEP 8 guidelines may not be re-formatted, but it doesn’t mean that the code looks good. Its algorithm takes the code and reformats it to the best formatting that conforms to the style guide, even if the original code didn’t violate the style guide. This idea is similar to tools like clang-format or gofmt.
How to Use YAPF?
Install the latest version of YAPF from PyPI:
pip install yapf
And then, run YAPF on one or many files. On doing so, YAPF will format the files in-place.
How to Use Code Formatters With DeepSource?
If you use DeepSource for static analysis and automated code reviews, you can use Transformers to automate running one or more of these code-formatters in your
.deepsource.toml file. For instance:
name = "black"
enabled = true
The full list of Transformers is available in the docs.
Published at DZone with permission of Sanket Saurav. See the original article here.
Opinions expressed by DZone contributors are their own.