Reputation: 304
I have properly installed all of the aforementioned modules on a VM I use on Ubuntu 18.04. When running either of them on a specific script or folder, they do correctly identify style errors and output them in the console. E.g.:
(venv) .../src$ python3.6 -m flake8
./free_prediction.py:8:1: E303 too many blank lines (5)
./free_prediction.py:8:28: E231 missing whitespace after ','
./free_prediction.py:10:5: E225 missing whitespace around operator
./free_prediction.py:12:3: E225 missing whitespace around operator
./free_prediction.py:15:13: E225 missing whitespace around operator
However, the same style errors persist in the code. How can I make them to be automatically corrected?
Upvotes: 18
Views: 32048
Reputation: 5405
Find the corresponding Pylint rules in ruff
from https://github.com/astral-sh/ruff/issues/970, then use ruff
to fix it.
For example: ruff check . --select ISC001 --fix
fixes the W1404 implicit-str-concat
error in Pylint.
ruff
also implements flake8 rules.
Ref:
Upvotes: 0
Reputation: 187
Flake8 is growing in attraction.
Hence, the situation has changed a little bit since 2019. I consider it worth noting that there is now also a project for fixing flake8
errors (e.g., McCane complexity) additionally to pycodestyle
errors (PEP8):
pycodestyle
- https://pypi.org/project/autopep8/flake8
(wrapper around pycodestyle
, pyflakes
, and mccabe
) - https://pypi.org/project/autoflake8/Upvotes: 4
Reputation: 5992
The solution you are looking for is called an "auto-formatter."
The tools you mentioned are just for checking for the style of the code.
See this blog post for a thorough comparison of black, autopep8, and yapf against actual Python snippets.
https://medium.com/3yourmind/auto-formatters-for-python-8925065f9505
Upvotes: 8
Reputation: 60604
AFAIK, none of those linting tools will fix the style issues they identify. However, there are several code formatting tools that will automatically fix many of the style errors that were flagged.
Some of the more popular Python code formatting tools worth checking out are: black
, autopep8
, and yapf
. (all of them are on PyPI and installable via pip)
More info:
Upvotes: 24