pylint
Static Code Analysis for Better Code Quality
pylint
is a static code analyzer that checks Python source code for errors, enforces coding standards, and suggests improvements for readability and maintainability.
Install pylint
using pip
:
pip install pylint
Verify installation:
pylint --version
pylint your_script.py
.pylintrc
config file.pylint categorizes messages by type and provides specific message codes. Here are some common categories and examples:
C0103
- Invalid naming style (e.g., variable or function name not snake_case)C0114
- Missing module docstringC0115
- Missing class docstringR0913
- Too many arguments in functionR1705
- Simplify conditional expressionW0611
- Unused importW0612
- Unused variableE1101
- Module has no member (possible typo or wrong import)E0001
- Syntax errorF0001
- Fatal error (e.g., invalid Python version)# bad_script.py
def myFunction():
print("Hello") # Indentation error and naming style warning
Running pylint bad_script.py
highlights errors and warnings.
def foo():
x = 1 # pylint: disable=unused-variable
print("Foo called")
This disables the warning about the unused variable x
for that line.
.pylintrc
File[MESSAGES CONTROL]
disable=C0114, C0115, C0116 # disable missing docstring warnings
Place this file in your project root to customize pylint's checks globally.
Using pylint
regularly helps maintain clean, readable, and error-free Python code.