pycodestyle: Python Style Guide Checker
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
- breaking The package was renamed from `pep8` to `pycodestyle`. Projects still importing `pep8` will fail when upgrading or installing `pycodestyle`.
- breaking `pycodestyle` dropped support for older Python versions. As of version 2.14.0, it requires Python 3.9 or newer.
- gotcha Configuration files (`setup.cfg`, `tox.ini`, `pyproject.toml`) now expect a `[pycodestyle]` section for configuration, not `[pep8]`.
- 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`).
Install
-
pip install pycodestyle
Imports
- pycodestyle
import pycodestyle
- StyleGuide
from pycodestyle import StyleGuide
- Checker
from pycodestyle import Checker
Quickstart
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)