Flake8 tool to check python source code

Flake8 is a wrapper around several tools:

  • PyFlakes:
  • check source files for errors.
  • pycodestyle:
  • check source code against PEP8 conventions.
  • Ned Batchelder’s McCabe script:
  • perform McCabe complexity check.

Source code in GitLab repo:

Flake8 documentation:

Flake8 quickstart:


Install flake8 using pip installer:
$ pip install flake8

Show help:


$ flake8 --help
usage: flake8 [options] file file ...

positional arguments:

optional arguments:
-h, --help show this help message and exit
-v, --verbose Print more information about what is happening in flake8. This option is repeatable and will increase verbosity each time it is repeated.
--output-file OUTPUT_FILE

Execute flake8 to test one or several files:

$ flake8 main.py

$ flake8 main.py database.py

or for the whole project in current directory:

$ flake8 .
./main.py:18:1: F401 'wtforms.StringField' imported but unused
./main.py:186:13: F821 undefined name 'l'
./main.py:327:9: F841 local variable 'save' is assigned to but never used
./database.py:5:1: F401 'logging' imported but unused
./database.py:8:1: F401 'logging.warning as lwarn' imported but unused
./database.py:10:1: F401 'logging.exception as lexcept' imported but unused

Check for several specific errors or warnings:
$ flake8 --select E303,E501 main.py
main.py:5:80: E501 line too long (92 > 79 characters)
main.py:6:80: E501 line too long (94 > 79 characters)

Ignore several warnings or errors:
$ flake8 --ignore E501 main.py

We can create a configuration file (using INI format):

E.g: .flake8 in the project directory:

ignore = E501,E303

select = E,W

exclude = .git,

max-line-length = 100

max-complexity = 10

We can execute $ flake8 and it will read options from config file .flake8

Command line options override configuration file ones.