16

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?

Jan
  • 264
  • 1
  • 5
  • 14

3 Answers3

24

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:

Corey Goldberg
  • 59,062
  • 28
  • 129
  • 143
8

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

Kermit
  • 4,922
  • 4
  • 42
  • 74
4

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

notfancy
  • 157
  • 1
  • 7