PEP 8 (legacy package)
The `pep8` package, version 1.7.1, is a Python style guide checker that verifies code against some of the conventions in PEP 8. This package has been renamed to `pycodestyle` to reduce confusion between the tool and the actual PEP 8 style guide document. While still installable, users are strongly encouraged to use `pycodestyle` instead. The package is in maintenance mode under its old name, with active development continuing as `pycodestyle`.
Warnings
- breaking The `pep8` package has been renamed to `pycodestyle`. While `pep8` (version 1.7.1) is still available on PyPI, it is no longer actively maintained under this name. New features and bug fixes are exclusively released in `pycodestyle`.
- gotcha The `pep8` command-line tool and its programmatic interface directly print style errors to standard output. This can make it challenging to capture and process errors programmatically without redirecting output or using `pycodestyle`'s more flexible API.
- gotcha Mixing tabs and spaces for indentation is a common PEP 8 violation that often leads to hard-to-debug `IndentationError` in Python 3. PEP 8 strongly recommends using 4 spaces per indentation level.
Install
-
pip install pep8 -
pip install pycodestyle
Imports
- Checker
from pep8 import Checker
Quickstart
import os
# Create a dummy Python file to check
with open('example.py', 'w') as f:
f.write('def my_function(a,b):\n return a+ b\n')
# Command-line usage (typical)
# import subprocess
# subprocess.run(['pep8', 'example.py'])
# Programmatic usage of the legacy pep8 package
from pep8 import Checker
# You can pass a filename or a file-like object
checker = Checker('example.py')
errors = checker.check_all()
print(f"Found {errors} style errors.")
if errors > 0:
print("Note: The pep8 package prints errors directly to stdout during check_all().")
# Cleanup (optional)
os.remove('example.py')