LinuxCommandLibrary

flake8

Check Python code style and detect errors

TLDR

Lint a file or directory recursively

$ flake8 [path/to/file_or_directory]
copy

Lint a file or directory recursively and show the line on which each error occurred
$ flake8 --show-source [path/to/file_or_directory]
copy

Lint a file or directory recursively and ignore a list of rules. (All available rules can be found at )
$ flake8 --ignore [rule1,rule2,...] [path/to/file_or_directory]
copy

Lint a file or directory recursively but exclude files matching the given globs or substrings
$ flake8 --exclude [substring1,glob2] [path/to/file_or_directory]
copy

SYNOPSIS

flake8 [OPTIONS] [FILENAMES | DIRECTORIES]

PARAMETERS

-h, --help
    Show help message and exit

--version
    Show program's version number and exit

--config=CONFIG
    Path to custom config file

--doctests
    Check syntax errors in docstrings

--exclude=EXCLUDE
    Exclude directories/files (comma-separated)

--extend-exclude=EXTEND_EXCLUDE
    Extend exclude list with globs

--filename=FILENAME
    Check only files matching regex

--format=FORMAT
    Custom output format string

--ignore=IGNORE
    Ignore specific error codes (comma-separated)

--max-complexity=MAX_COMPLEXITY
    Set McCabe complexity threshold (default: 10)

--max-line-length=n
    Maximum line length (default: 79)

--select=SELECT
    Select specific error codes only

--show-pep8
    Show pep8/pycodestyle violations

--show-source
    Include source code snippet with errors

--statistics
    Count errors/warnings by type

-v, --verbose
    Increase verbosity (repeatable)

-q, --quiet
    Reduce verbosity (repeatable)

--jobs=JOBS
    Run with multiple processes (default: 1)

DESCRIPTION

Flake8 is a popular Python source code linting tool that wraps three individual linters: PyFlakes for logical errors, pep8 (now pycodestyle) for style guide enforcement per PEP 8, and McCabe for cyclomatic complexity analysis.

It scans Python files for syntax errors, undefined names, unused imports, style violations like incorrect indentation or line lengths, and high complexity functions. Output includes error codes, line numbers, and messages for quick fixes.

Highly configurable via command-line options, configuration files (setup.cfg, tox.ini, .flake8, or pyproject.toml), or plugins. Integrates with editors like VS Code, Vim, and CI tools like GitHub Actions or Jenkins. Supports ignoring/selecting specific errors, custom formats, and parallel processing for large codebases.

Ideal for maintaining code quality in projects, enforcing team standards, and catching issues early. Lightweight and fast, it doesn't modify code but reports issues via stdout or files.

CAVEATS

Not installed by default; requires pip install flake8. Python 3.8.1+ only. No auto-fixing (use black or autopep8). Config files override CLI options.

INSTALLATION

pip install flake8 or pipx install flake8 for isolation.

EXIT CODES

0: no errors; 1: some errors; other: usage error or crash.

CONFIGURATION

Use [flake8] section in setup.cfg, tox.ini, .flake8, or pyproject.toml.

HISTORY

Created in 2011 by Tarek Ziade as a wrapper for PyFlakes, pep8, and McCabe. Became popular for simplicity over pylint. Maintained by PyCQA since 2016; current version 7.x supports plugins and modern Python.

SEE ALSO

pylint(1), pycodestyle(1), pyflakes(1), mypy(1)

Copied to clipboard