Python Standard Library Overview: Which Module to Use
The Python standard library, grouped by job: text, collections, numbers, dates, files, formats, concurrency and testing, with the module to reach for in each.
- Course: Python study plan
- Module: Modules, packages and imports
- Kind: Lesson
- Reading time: 14 min
- Runtime: CPython 3.11
What is in the Python standard library?
The Python standard library is the set of modules that ships with every Python installation, the "batteries included". It covers text (re, textwrap), data structures (collections, itertools), numbers (math, statistics, decimal), dates (datetime, zoneinfo), files (pathlib, shutil), formats (json, csv, sqlite3), concurrency (asyncio, threading), networking and testing (unittest).
Lesson
"Batteries included" is Python's oldest slogan, and the batteries are the reason a Python program that parses a date, reads a CSV, hashes a file and serves it over HTTP is a hundred lines rather than a thousand. The skill is knowing the map: which module holds the thing you need, so that you look there before writing it yourself. This lesson is that map — the modules grouped by job, with the one or two functions each is used for — plus how to read the documentation and how to explore a module from the REPL. Later modules of this track go deep on the areas that matter most; this one is the index.
Text
| Module | For |
|---|---|
string | ascii_letters, digits, punctuation, Template |
re | regular expressions (Module 5) |
textwrap | wrap, fill, dedent, shorten, indent |
difflib | SequenceMatcher, unified_diff, get_close_matches |
unicodedata | normalize, name, category |
pprint | pprint, pformat for nested data |
Data structures and functional tools
| Module | For |
|---|---|
collections | Counter, defaultdict, deque, namedtuple, OrderedDict, ChainMap |
itertools | chain, islice, groupby, product, combinations, accumulate (Module 11) |
functools | cache, partial, reduce, wraps, singledispatch (Module 11) |
operator | itemgetter, attrgetter, the operators as functions |
heapq, bisect | priority queues, binary search in sorted lists (Module 7) |
enum | Enum, IntEnum, Flag, auto (Module 13) |
dataclasses | dataclass, field, asdict (Module 8) |
copy | copy, deepcopy |
array | compact homogeneous numeric arrays |
Numbers
| Module | For |
|---|---|
math | sqrt, gcd, lcm, isqrt, comb, factorial, log, floor, ceil, isclose, inf, pi |
statistics | mean, median, mode, stdev, quantiles |
fractions | Fraction — exact rationals |
decimal | Decimal — decimal floating point for money |
random | Random(seed), randint, choice, shuffle, sample |
secrets | cryptographically strong tokens and choices |
numbers | the numeric ABCs (Number, Integral) |
Dates and times
| Module | For |
|---|---|
datetime | date, time, datetime, timedelta, strptime/strftime (Module 13) |
zoneinfo | IANA time zones: ZoneInfo("Asia/Kolkata") |
time | perf_counter, monotonic, sleep, time |
calendar | month tables, leap years, weekday names |
Files, paths and the OS
| Module | For |
|---|---|
pathlib | Path — the object-oriented file system API (Module 14) |
os | environ, getcwd, listdir, process and OS calls |
os.path | the older string-path functions |
shutil | copy, move, rmtree, which, disk usage |
glob, fnmatch | wildcard file matching |
tempfile | TemporaryDirectory, NamedTemporaryFile |
io | StringIO, BytesIO — in-memory files |
sys | argv, stdin/stdout/stderr, exit, path, version_info, setrecursionlimit |
subprocess | run other programs: run(["ls", "-l"], capture_output=True, text=True) |
argparse | command-line parsing (lesson 5) |
logging | structured logging (Module 15) |
Data formats and persistence
| Module | For |
|---|---|
json | loads/dumps, load/dump (Modules 7, 14) |
csv | reader, writer, DictReader, DictWriter (Module 14) |
tomllib | read TOML (3.11); writing needs a third-party package |
configparser | INI files |
pickle | Python-object serialisation — never load untrusted data |
sqlite3 | an embedded SQL database (Module 14) |
struct | pack/unpack binary records |
base64, hashlib, hmac, zlib, gzip, zipfile, tarfile | encodings, digests, compression, archives |
xml.etree.ElementTree, html | XML parsing, HTML escaping |
Concurrency and networking
| Module | For |
|---|---|
threading, queue | threads, locks, thread-safe queues (Module 17) |
multiprocessing, concurrent.futures | processes and pools (Module 17) |
asyncio | the async event loop (Module 17) |
socket, ssl, selectors | low-level networking |
http.client, http.server, urllib.request | HTTP without third-party packages |
email, smtplib | building and sending mail |
Language, testing and tooling
| Module | For |
|---|---|
typing, abc, collections.abc | hints, ABCs, protocols (Modules 9, 15) |
contextlib | context-manager helpers (Module 10) |
unittest, unittest.mock, doctest | testing (Module 18) |
traceback, warnings, pdb, inspect | debugging and introspection |
timeit, cProfile, tracemalloc, dis | measurement (Module 19) |
importlib | import by name, package resources |
ast | parse Python source; literal_eval |
venv, ensurepip | environments (lesson 4) |
Reading the documentation
Every module's page at docs.python.org/3/library/ follows one shape: a summary, the functions and classes with their signatures and version notes ("Changed in version 3.10"), then examples. Read the signature, then the version note if you support older interpreters, then the example. In the REPL, help(module) prints the docstrings, dir(module) lists the names, and help(module.function) shows the signature — enough for most questions without leaving the terminal. For a function whose behaviour you are unsure of, the fastest check is to call it on a small value and look.
The habit
Before writing a helper, ask: is this a text job (textwrap, difflib), a collection job (collections, itertools), a number job (math, statistics), a file job (pathlib, shutil), a format job (json, csv)? Ten minutes with the map saves an hour and a bug: the library's version has been tested on the edge cases you have not thought of yet.
Pitfalls
- Writing a mean, a deque, a permutations generator or a CSV parser by hand.
pickle.loadon data from outside your program.time.timewhereperf_counter(timing) ormonotonic(timeouts) was meant.os.pathstring juggling wherepathlibreads better.- Reaching for a third-party package before checking the standard library.
- Guessing at behaviour instead of a two-line REPL experiment.
Key takeaways
- The standard library covers text, collections, numbers, dates, files, formats, concurrency, networking, testing and tooling; know the map before writing helpers.
collections,itertools,functools,pathlib,json,csv,datetime,re,math,logging,unittestare the modules every program touches.help(),dir()and a small experiment in the REPL answer most questions; the docs show signature, version notes, examples.pickleis for trusted data only;secretsfor anything security-related;perf_counterfor timing.- Prefer the library's tested implementation to a hand-written one.
Common questions
What does "batteries included" mean in Python?
It is Python's long-standing slogan for its large standard library: an installation already includes modules for parsing dates, reading CSV and JSON, hashing files, regular expressions, SQLite databases and serving HTTP, so many programs need no third-party packages at all.
How do I find what functions a Python module has?
In the REPL, dir(module) lists its names, help(module) prints its docstrings and help(module.function) shows one signature. The documentation at docs.python.org/3/library/ gives each module's functions with signatures, version notes such as "Changed in version 3.10" and examples. A two-line experiment on a small value settles the rest.
Is pickle safe to use in Python?
Only for data your own program wrote. Unpickling can run arbitrary code while it rebuilds objects, so never call pickle.load on data from outside a trust boundary. Use json for data exchanged with other programs, and tomllib or configparser for configuration files.
Which Python module should I use to time code?
time.perf_counter() for measuring elapsed time, since it has the highest available resolution and is not affected by changes to the system clock; time.monotonic() for timeouts; time.time() only for wall-clock timestamps. For benchmarking small snippets, the timeit module runs the code many times.
How do I run a shell command from Python?
Use subprocess.run with the command as a list of arguments, for example subprocess.run(["ls", "-l"], capture_output=True, text=True), which returns the exit code and the captured output. Passing a list rather than a string built from input avoids shell injection.
Exercises
Import census
Read Python import statements — import a.b as c, import x, y, from p.q import r — and classify each imported top-level module as stdlib (its first component is in sys.stdlib_module_names) or other. Print the distinct top-level names of each group, sorted, then how many statements there were.
Input: import statements, one per line. Output: stdlib: <names> (or stdlib: none), other: <names> (or other: none), statements <n>.
import os.path as osp
from collections import Counter
import numpy as np, json
from shop.models import Product
prints
stdlib: collections json os
other: numpy shop
statements 4Explore a module
Read a standard-library module name and a prefix. Import the module with importlib.import_module, list its public names (dir, excluding names starting with _) that start with the prefix, and print the callables sorted, then the non-callables sorted ((none) for an empty group).
Input: module prefix. Output: callable: … then other: ….
math is
prints
callable: isclose isfinite isinf isnan isqrt
other: (none)In this module: Modules, packages and imports
- Modules and imports — files, namespaces and sys.modules
- Packages — directories, __init__.py, relative imports and project layout
- The standard library map — where to look before you write it (this lesson)
- Virtual environments and packaging — venv, pip and pyproject.toml
- Scripts and the command line — argv, argparse, exit codes and streams
- Checkpoint — Modules, packages and imports
← Packages — directories, __init__.py, relative imports and project layout · Virtual environments and packaging — venv, pip and pyproject.toml →