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] inputfile ...

PARAMETERS

--version
    Show program's version number and exit

--verbose
    Report all level of errors.

--repeat
    Show all occurrences of the same error

--ignore=E121,E123
    Ignore errors and warnings.
For example: --ignore=E4,W

--select=E121,E123
    Select errors and warnings.
For example: --select=E4,W

--max-line-length=120
    Set maximum allowed line length.
The default is 79 for code and 72 for comments

--hang-closing
    Hang closing bracket instead of matching indentation of opening bracket's line.

--exclude=test.py,tests/
    Exclude file or directories.
For example: --exclude=test.py,tests/

--filename=*.py
    Report only files matching this pattern.

--format=pylint
    Set output format (default: human-readable text)

--diff
    Report only lines changed since last commit/save

--count
    Print total number of errors and warnings to standard output

--statistics
    Count errors and warnings.

DESCRIPTION

pycodestyle (formerly pep8) is a tool that checks Python code against some of the style conventions in PEP 8, the Python style guide. It helps developers write more readable and consistent code by identifying style issues such as line length violations, indentation problems, and missing whitespace. By enforcing a consistent style, pycodestyle improves code maintainability and collaboration. It's widely used in Python projects to ensure code quality and adherence to community standards. The tool is configurable to allow for ignoring specific rules or customizing behavior based on project requirements. It integrates seamlessly with many editors and IDEs, making it easy to incorporate style checks into the development workflow. pycodestyle strives to be a relatively simple tool, offering a focused set of checks for common style issues, unlike more comprehensive linters like pylint. The output is designed to be easily understood and actionable. It can be used directly from the command line or integrated into automated build processes.

CAVEATS

pycodestyle only checks style; it doesn't perform static analysis or detect potential runtime errors.
It relies on accurate parsing of the Python code, so syntax errors can cause incorrect results.

EXIT CODES

pycodestyle returns 0 on success (no style violations) and 1 if any style violations are found.

HISTORY

Originally named 'pep8', the tool was renamed to pycodestyle to better reflect its broader role as a code style checker, not solely focused on PEP 8 adherence. The renaming occurred because the tool evolved to incorporate checks beyond those explicitly defined in PEP 8. The initial development was driven by the need for a simple, focused tool to enforce style consistency in Python projects. Over time, it has gained widespread adoption in the Python community and has been integrated into various development workflows and tools. Continued development focuses on improving accuracy, adding new checks, and enhancing integration with other tools.

SEE ALSO

pylint(1), flake8(1)

Copied to clipboard