Check Your Code With pycodestyle
PEP 8 is the official style guide for Python, outlining conventions and best practices to ensure Python code is readable. Tool like pycodestyle can be helpful for beginners who have just started programming journey in Python.
In the world of agentic coding, coding agents like Claude Code and Codex, along with LLMs, can generate accurate code. However, sometimes we need to make manual changes to the code. When that happens, running a simple pycodestyle check in the terminal might save us time later.
Today, we will look at pycodestyle to help us follow PEP 8 best practices. You can also check this Link to get started.
Installation
To install or upgrade pycodestyle on your system, run the following commands:
$ pip install pycodestyle # to install
$ pip install --upgrade pycodestyle # to upgradeUsage I
Create a file named test.py and add the following code:
#Print hello world
print('Hello World' )There are some errors in this code. We can fix them manually, but in large codebases, we might miss some. In those cases, we can use pycodestyle to check our code’s style.
Using pycodestyle to detect flaws
$ pycodestyle test.pyAfter running pycodestyle test.py in the terminal, you will see the following output:
test.py:1:1: E265 block comment should start with '# '
test.py:2:5: E113 unexpected indentation
test.py:2:24: E202 whitespace before ')'Here, we can see the potential errors in the test.py file.
Usage II
We can check multiple files for PEP 8 style errors as follows:
# check_style.py
# Import needed package
import pycodestyle
# Create a StyleGuide instance
style_checker = pycodestyle.StyleGuide()
# Run PEP 8 check on multiple files
result = style_checker.check_files(['test.py', 'test2.py'])
# Print result of PEP 8 style check
print(result.messages)After running python check_style.py in the terminal, you will see the following output:
test.py:1:1: E265 block comment should start with '# '
test.py:2:5: E113 unexpected indentation
test.py:2:24: E202 whitespace before ')'
test2.py:1:22: E231 missing whitespace after ','
test2.py:2:3: E111 indentation is not a multiple of 4
test2.py:2:8: E225 missing whitespace around operator
test2.py:3:3: E111 indentation is not a multiple of 4
test2.py:3:17: E231 missing whitespace after ','
test2.py:4:3: E111 indentation is not a multiple of 4
test2.py:6:1: E305 expected 2 blank lines after class or function definition, found 1
test2.py:6:8: E225 missing whitespace around operator
test2.py:6:11: E231 missing whitespace after ','
test2.py:6:13: E231 missing whitespace after ','
test2.py:8:3: E111 indentation is not a multiple of 4
test2.py:8:21: E231 missing whitespace after ','
{'E265': "block comment should start with '# '", 'E202': "whitespace before ')'", 'E113':
'unexpected indentation', 'E231': "missing whitespace after ','", 'E111':
'indentation is not a multiple of 4','E225': 'missing whitespace around operator', 'E305':
'expected 2 blank lines after class or function definition, found 1'}Uninstall
To uninstall pycodestyle, run the following command in the terminal:
$ pip uninstall pycodestyle