LinuxCommandLibrary

pycodestyle

Check Python code style against PEP 8

TLDR

Check the style of a single file

$ pycodestyle [file.py]
copy

Check the style of multiple files
$ pycodestyle [file1.py file2.py ...]
copy

Show only the first occurrence of an error
$ pycodestyle --first [file.py]
copy

Show the source code for each error
$ pycodestyle --show-source [file.py]
copy

Show the specific PEP 8 text for each error
$ pycodestyle --show-pep8 [file.py]
copy

SYNOPSIS

pycodestyle [options] [files or directories ...]

PARAMETERS

--first
    Show only the first occurrence of each error.

--show-source
    Display the source code line for each error.

--statistics
    Print count of errors and warnings.

--count
    Print the total number of errors and warnings.

--max-line-length
    Set the maximum allowed line length (default 79).

--ignore
    Exclude specific error codes (e.g., E501, W292).

--select
    Select specific error codes to report.

--exclude
    Exclude files or directories matching patterns.

--diff
    Report errors only on changed lines in a diff.

--config
    Specify a custom path to a configuration file.

--format
    Set the error output format (e.g., default, pylint).

--version
    Show program's version number and exit.

--verbose
    Print status messages and more detailed output.

DESCRIPTION

pycodestyle is a command-line tool used to check Python code against the PEP 8 style guide. It analyzes source files for common stylistic and programmatic errors, ensuring consistency and readability across projects. Originally known as pep8, it was renamed to avoid confusion with the PEP 8 document itself. Its primary purpose is to enforce a standardized coding style, making code easier to read, understand, and maintain for developers. It can be integrated into development workflows, CI/CD pipelines, or run manually during development. It helps identify issues like incorrect indentation, trailing whitespace, naming conventions, and line length violations.

CAVEATS

pycodestyle focuses strictly on PEP 8 style and may not catch all logical or semantic errors in code. Its strictness can sometimes lead to numerous warnings, which might require careful configuration using ignore lists or project-specific settings. It can also produce false positives for certain complex code structures. Effective use often involves integration with configuration files like setup.cfg or pycodestyle.ini to tailor its behavior to specific project needs.

CONFIGURATION FILES

pycodestyle supports configuration via standard Python project files such as setup.cfg, tox.ini, or a dedicated pycodestyle.ini file. These files allow users to define default options like max-line-length, ignore codes, or exclude paths, enabling consistent style checks across a project without repetitive command-line arguments.

EXIT STATUS

The command exits with a status of 0 if no errors or warnings are found in the checked files. A non-zero exit status (typically 1) indicates that one or more issues were detected. This behavior makes pycodestyle suitable for use in automated scripts and CI/CD pipelines to enforce coding standards.

HISTORY

The tool was originally developed by Florent Aide and was known as pep8.py or simply pep8. It was created to provide a command-line utility for checking Python code against the PEP 8 style guide, which defines conventions for Python code layout. The project was later renamed to pycodestyle in 2016-2017 to clarify that it is an implementation of the PEP 8 checks, not the PEP 8 document itself, and to avoid naming conflicts, especially as PEP 8 is a living document.

SEE ALSO

flake8(1): A wrapper for pycodestyle, pyflakes, and McCabe complexity checks., pylint(1): A comprehensive static code analyzer for Python., black(1): An uncompromising Python code formatter., isort(1): A Python utility to sort imports alphabetically and automatically separate into sections.

Copied to clipboard