pycodestyle: Python Style Guide Checker

raw JSON →
2.14.0 verified Tue May 12 auth: no python install: verified quickstart: verified

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.

pip install pycodestyle
error E501 line too long (X > Y characters)
cause A line of code exceeds the maximum allowed character length, which defaults to 79 characters according to PEP 8.
fix
Refactor the long line into multiple shorter lines, use implicit string concatenation for long strings, or configure pycodestyle to increase the --max-line-length (e.g., pycodestyle --max-line-length=120) or ignore the error with # noqa: E501 on the line.
error W292 no newline at end of file
cause The Python source file does not end with a single blank line.
fix
Add a blank line at the very end of your .py file.
error E401 multiple imports on one line
cause Multiple modules are imported on a single line using a comma-separated list, which violates PEP 8.
fix
Place each import statement on its own separate line.
error F401 'module' imported but unused
cause A module or name has been imported into the file but is not subsequently used within the code. While technically a Pyflakes error, it's commonly reported when using tools like Flake8, which integrates pycodestyle and pyflakes.
fix
Remove the unused import statement. If it's an __init__.py file and the import is intended for re-export, add the name to the module's __all__ variable to explicitly declare it as part of the public API, or use # noqa: F401 to suppress the warning for that specific line.
breaking The package was renamed from `pep8` to `pycodestyle`. Projects still importing `pep8` will fail when upgrading or installing `pycodestyle`.
fix Update all `import pep8` statements to `import pycodestyle` or `from pycodestyle import ...`.
breaking `pycodestyle` dropped support for older Python versions. As of version 2.14.0, it requires Python 3.9 or newer.
fix Ensure your project is running on Python 3.9 or a newer compatible version. Consider using `pyenv` or virtual environments to manage Python versions.
gotcha Configuration files (`setup.cfg`, `tox.ini`, `pyproject.toml`) now expect a `[pycodestyle]` section for configuration, not `[pep8]`.
fix Rename `[pep8]` sections in your configuration files to `[pycodestyle]`.
gotcha `pycodestyle` only checks for a subset of PEP 8 style conventions (e.g., indentation, whitespace, line length). It deliberately does *not* cover other aspects like naming conventions or docstring conventions. For a more comprehensive linting solution, consider tools like `flake8` (which uses `pycodestyle` internally) with appropriate plugins (e.g., `pep8-naming`, `pydocstyle`).
fix Integrate `flake8` with relevant plugins for broader code quality checks if naming or docstring conventions are desired. `pip install flake8 pep8-naming pydocstyle`.
python os / libc status wheel install import disk
3.10 alpine (musl) - - 0.06s 17.9M
3.10 slim (glibc) - - 0.03s 18M
3.11 alpine (musl) - - 0.12s 19.8M
3.11 slim (glibc) - - 0.06s 20M
3.12 alpine (musl) - - 0.10s 11.7M
3.12 slim (glibc) - - 0.07s 12M
3.13 alpine (musl) - - 0.06s 11.3M
3.13 slim (glibc) - - 0.07s 12M
3.9 alpine (musl) - - 0.06s 17.4M
3.9 slim (glibc) - - 0.04s 18M

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)