How to make pylint, flake8 or pycodestyle to automatically correct errors?

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


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:

  • black: https://pypi.org/project/black/
  • autopep8: https://pypi.org/project/autopep8/
  • yapf: https://pypi.org/project/yapf/

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):

  • autopep8: Tries to resolve issues found by pycodestyle - https://pypi.org/project/autopep8/
  • autoflake8: Tries to resolve issues found by flake8 (wrapper around pycodestyle, pyflakes, and mccabe) - https://pypi.org/project/autoflake8/