flake8#

Flake8 is one of the many projects backed by the Python Code Quality Authority. What makes flake8 particularly useful is that it glues together several tools:

  • pycodestyle to check against PEP 8 style conventions

  • pyflakes to check for syntax errors and unused variables or imports

  • mccabe to check the cyclomatic complexity of the code

Especially when tightly integrated with the editor, linters can offer easy rewards for low effort. They can quickly identify syntax errors and help to avoid common bugs and pitfalls.

Usage#

Use the flake8 command to check a given file

$ flake8 some_file.py
$ flake8 --max-line-length=100 some_file.py

When checking a whole folder you often need to exclude the virtual environment

$ flake8 --exclude .venv .

There are different ways to ignore certain violations. Either globally by adding the error code to the ignore list

$ flake8 --ignore=E402 some_file.py

or locally in the code using a noqa, short for “no quality assurance”, comment

import os  # noqa: E402

If you want to use flake8 with VSCode, add the following lines to your settings.json:

"python.linting.flake8Enabled": true,
"python.linting.flake8Args": [
	"--max-line-length=100"
],