TL;DR: Use Black for code formatting, isort for import sorting, and Flake8 for linting. This combination ensures consistent, readable code across all Python projects.
Different developers have different coding habits -- variable naming, indentation style, import ordering, and more. Python's PEP 8 standard defines a comprehensive set of rules for consistent code styling. You do not need to memorize PEP 8; tooling handles enforcement automatically.
| Tool | Purpose | Customizability |
|---|---|---|
| Black | PEP 8-compliant code formatter | Minimal (by design -- ensures all code looks the same) |
| isort | Import sorter (stdlib, third-party, application) | Moderate (supports Black-compatible formatting) |
| Flake8 | Linter for PEP 8 compliance and code quality | High |
| YAPF | PEP 8-compliant formatter by Google | Very high (multiple base styles) |
| autopep8 | PEP 8 auto-fixer | Moderate |
Black's strict, opinionated formatting means all code formatted by Black looks identical. This consistency reduces cognitive overhead during code reviews and makes unfamiliar codebases immediately readable. Black is also trivial to set up -- there are no configuration knobs to tune.
YAPF (Google-style formatting):
records = get_records(args.key, feed, args.output, args.range,
args.field, args.extra, args.expiration)Black formatting:
records = get_records(
args.key,
feed,
args.output,
args.range,
args.field,
args.extra,
args.expiration,
)isort organizes imports into three sections (standard library, third-party, and application-specific), sorts them lexicographically and case-insensitively, and supports Black-compatible formatting:
import argparse
import math
import multiprocessing
from typing import Type, Union
import cv2
import ffmpeg
from loguru import logger
from rich.progress import (
BarColumn,
Progress,
ProgressColumn,
)
from rich.text import Text
from . import __version__
from .decoder import VideoDecoder
from .encoder import VideoEncoderRecommended extensions and settings:
- Python (includes Pylance)
- Python Indent
- autoDocstring
{
"python.formatting.provider": "black",
"python.languageServer": "Pylance",
"python.linting.flake8Enabled": true,
"python.analysis.typeCheckingMode": "strict"
}Recommended plugins:
- nvie/vim-flake8 -- Flake8 linting
- psf/black -- Black formatter integration
- neoclide/coc.nvim -- Extension loader (use
:CoCInstall coc-pyrightfor Python support) - dense-analysis/ale -- Asynchronous linting engine
- kkoomen/vim-doge -- Documentation generation
ALE configuration for Black:
let g:ale_fixers = {
\ '*': ['trim_whitespace', 'prettier'],
\ 'python': ['black']
\}
let g:ale_completion_enabled = 1