Standard Library Overview
Python ships a large standard library - batteries included for paths, time, JSON, processes, parsing, and more. Reach for stdlib before adding dependencies; know which module owns each job.
Recipe
from pathlib import Path
import json
import logging
log = logging.getLogger(__name__)
config = json.loads(Path("config.json").read_text(encoding="utf-8"))
log.info("loaded keys=%s", list(config))When to reach for stdlib:
- File paths ->
pathlib - CLI args ->
argparse - HTTP client for scripts -> consider stdlib
urllibor httpx if needs grow - Hashing/tokens ->
hashlib,secrets - Subprocess orchestration ->
subprocess
Working Example
Map common tasks to modules in one script.
from __future__ import annotations
import json
import re
import secrets
import subprocess
import sys
from datetime import datetime, timezone
from pathlib import Path
def snapshot_system(tmp: Path) -> dict:
return {
"python": sys.version.split()[0],
"cwd": str(Path.cwd()),
"now_utc": datetime.now(timezone.utc).isoformat(),
"token": secrets.token_hex(8),
"digits": re.findall(r"\d+", "a1b22"),
"ls": subprocess.run(
["ls", str(tmp)],
capture_output=True,
text=True,
check=False,
).stdout.strip(),
}
tmp = Path("/tmp")
print(json.dumps(snapshot_system(tmp), indent=2))What this demonstrates:
- Stdlib composes without pip install
- Each import maps to a stable platform capability
- Prefer typed, explicit wrappers in app code over one-off scripts only
- Upgrade to PyPI when stdlib lacks features (HTTP/2, async DB)
Deep Dive
Module Map (Core)
| Need | Module |
|---|---|
| Paths | pathlib, os |
| Time | datetime, zoneinfo |
| Data formats | json, tomllib (3.11+) |
| Text patterns | re |
| Processes | subprocess |
| CLI | argparse |
| Crypto-ish | hashlib, secrets, hmac |
When to Add PyPI
- httpx/aiohttp for production HTTP
- pydantic for validation
- requests ergonomic but sync-only
Gotchas
- Reinventing stdlib poorly - custom path join bugs. Fix: pathlib.
- Installing deps for one function -
more-itertoolsnice but itertools often enough. Fix: check stdlib first. - Ignoring tomllib for config - TOML is 3.11+ stdlib. Fix: pyproject alignment.
- urllib without timeouts - hangs. Fix: always set timeout or use httpx.
- subprocess shell=True - injection risk. Fix: list args,
shell=False.
Alternatives
| Alternative | Use When | Don't Use When |
|---|---|---|
| PyPI micro-lib | Stdlib missing feature | One-function need covered |
| Vendor copy | Restricted deploy | Normal network install ok |
FAQs
Is stdlib stable?
Modules evolve with deprecation cycles - read What's New per Python version (3.14).
Where is HTTP?
urllib.request for simple sync; production services use httpx/requests.
tomllib vs configparser?
tomllib for pyproject TOML; configparser for INI-style legacy config.
async in stdlib?
asyncio, async generators, some protocols - not all IO is async-native.
How to discover modules?
docs.python.org library index + this section's focused pages.
typing in stdlib?
typing, types, dataclasses, enum - no pydantic required for simple models.
Is pickle stdlib?
Yes pickle - avoid untrusted data; prefer json for interchange.
logging vs print?
logging for services; print for CLI user output and quick scripts.
venv in stdlib?
Yes - pair with uv 0.6+ externally for speed per team toolchain.
What's new in 3.14?
Check official 3.14 release notes for stdlib additions affecting your pins.
Related
- pathlib & os - filesystem
- json & Serialization - data exchange
- Standard Library Best Practices - selection rules
- argparse & configparser - CLI/config
Stack versions: This page was written for Python 3.14.0 (stable 3.14, maintenance 3.13), FastAPI 0.115+, Django 5.2, Flask 3.1, Pydantic 2, PyTorch 2.6+, pandas 2.2+, Polars 1.x, ruff 0.9+, and uv 0.6+.