pycodestyle: Python Style Guide Checker

2.14.0 · active · verified Sat Mar 28

pycodestyle is a tool designed to check Python code against a subset of the style conventions outlined in PEP 8. It's a widely adopted package within the Python ecosystem, helping developers maintain consistent and readable code. The current version is 2.14.0, and the project is actively maintained with regular updates.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to programmatically use `pycodestyle`'s `StyleGuide` class to check a Python file for PEP 8 compliance. It creates a temporary file with some common style violations, runs the checker, and reports the total number of errors found. You can also run `pycodestyle` directly from the command line: `pycodestyle your_file.py`.

import pycodestyle
import os

def create_example_file(filename):
    with open(filename, 'w') as f:
        f.write("import os, sys  # E401 multiple imports on one line\n")
        f.write("\n")
        f.write("def my_function(  ): # E201 whitespace after '('")
        f.write("    pass\n")

example_file = 'example_code.py'
create_example_file(example_file)

# Programmatic checking using StyleGuide
style_checker = pycodestyle.StyleGuide(quiet=True)
report = style_checker.check_files([example_file])

if report.total_errors:
    print(f"Found {report.total_errors} code style errors.")
    # For detailed output, you might remove quiet=True or configure the report object
    # For simplicity, this example only prints the total.
else:
    print("No code style errors found.")

# Clean up the example file
os.remove(example_file)

view raw JSON →