Python Standard Library Modules
By leveraging Python’s extensive standard library, you can ship scripts and services without extra dependencies. In these tutorials, you learn practical patterns for datetime, pathlib, collections, itertools, functools, argparse, logging, subprocess, json, and more. You get step-by-step examples, common pitfalls, and performance tips so you can write clean, fast, and idiomatic code.
Join Now: Click here to join the Real Python Newsletter and you’ll never miss another Python tutorial, course, or news update.
You see when to choose each module, how to combine them, and how to build cross-platform tools and tests. You also practice debugging and profiling with traceback, pdb, timeit, and unittest, plus modern typing with typing and dataclasses to keep your code safe and maintainable.
Use pathlib.Path for readable, cross-platform paths. Join with /, iterate with .glob(), and read or write using .read_text() and .write_text().
Load JSON from text with json.loads() or from files with json.load(). Serialize with json.dumps() or json.dump() and set indent=2 for pretty output. Validate keys using typing.TypedDict or dataclasses where helpful.
Yes, use argparse. Define a parser, call .add_argument(), then parse_args() to get typed values and automatic --help output.
Call subprocess.run([...], check=True, capture_output=True, text=True). Pass the command as a list of arguments to avoid invoking the shell. Use pathlib.Path objects for any file paths. Handle errors with subprocess.CalledProcessError.
Use datetime for time arithmetic, zoneinfo for IANA time zones and timezone.utc for UTC. Parse with .fromisoformat() and format with .strftime().