Introduction π
Linting & Formatting help you maintain clean, readable, and error-free code. They enforce consistent style and catch bugs before runtime.
Note
π‘ Formatter β auto-formats code
π‘ Essential for professional codebases, CI/CD, teams
1. Popular Linters & Formatters π οΈ
| Tool | Purpose |
|---|---|
| Flake8 | Linter (style + errors) |
| Pylint | Very strict linter |
| Black | Opinionated code formatter |
| isort | Sorts imports automatically |
| autopep8 | Auto-format to fit PEP8 |
2. Install Linting & Formatting Tools π¦
install_tools.sh
pip install flake8 black isort autopep8 pylint3. Using Flake8 (Linting) π
flake8_run.sh
flake8 my_project/β Checks for PEP8 violations, unused imports, undefined variables.
Common Errors
- E501 β Line too long
- F401 β Unused import
- F821 β Undefined variable
4. Flake8 Configuration βοΈ
setup.cfg
[flake8]
max-line-length = 100
exclude = .git,__pycache__,venv
ignore = E203, W503β Add to setup.cfg or .flake8
5. Using Black (Auto Formatter) π€
black_format.sh
black my_project/β Black reformats all code in a consistent way automatically.
Black Configuration
pyproject.toml
[tool.black]
line-length = 100
target-version = ['py39']
exclude = '''
/(
\.git
| \.pytest_cache
| venv
)/
'''6. Using isort (Sort Imports) π
isort.sh
isort my_project/pyproject.toml (isort)
[tool.isort]
profile = "black"β Ensures import order: stdlib β third-party β local
7. Using autopep8 (Alternative Formatter) β¨
autopep8.sh
autopep8 --in-place --recursive my_project/8. Using Pylint (Strict Linting) π§
pylint.sh
pylint my_project/Pylint scores your code (0β10) and reports detailed issues.
9. Editor Integration π§°
VS Code Settings Example
settings.json
{
"python.linting.enabled": true,
"python.linting.flake8Enabled": true,
"python.formatting.provider": "black",
"editor.formatOnSave": true
}β Formats on every save!
10. Pre-Commit Hooks (Automate linting before commit) πͺ
install_precommit.sh
pip install pre-commit.pre-commit-config.yaml
repos:
- repo: https://github.com/psf/black
rev: stable
hooks:
- id: black
- repo: https://github.com/PyCQA/flake8
rev: 6.1.0
hooks:
- id: flake8
- repo: https://github.com/pycqa/isort
rev: 5.12.0
hooks:
- id: isortprecommit_install.sh
pre-commit installβ Automatically formats & lints before every commit.
11. Example Workflow π§ͺ
Hereβs how a professional project handles linting & formatting:
- 1οΈβ£ Sort imports β isort
- 2οΈβ£ Format code β black
- 3οΈβ£ Lint code β flake8
- 4οΈβ£ Fix remaining issues
12. Example Bad vs Good Code π―
Bad Code
bad.py
import os,sys
def add(a, b):return a+bGood Code (Black + isort)
good.py
import os
import sys
def add(a, b):
return a + b13. Combined pyproject.toml Setup π
pyproject.toml
[tool.black]
line-length = 100
[tool.isort]
profile = "black"
[flake8]
max-line-length = 100
ignore = ["E203", "W503"]Best Practices π‘
- β Always format before committing
- β Use Black + isort together for consistency
- β Automate everything with pre-commit hooks
- β Configure flake8 to avoid false positives
- β Use Pylint only for strict code quality assessment
Conclusion π
You now understand Linting & Formatting in Python! Want the next topic? Try Testing (pytest), CI/CD Pipelines, Code Quality Tools, or Type Checking (Mypy). Just tell me! π