Skip to content

Stdlib, Testing & Tooling

Python ships with a philosophy known as “batteries included”: the standard library provides ready-made implementations for the tasks most programs need — no third-party packages required. When you install Python you automatically get modules for collections, data manipulation, networking, file I/O, testing, serialisation, concurrency, and much more.

The standard library spans more than 200 modules. Before reaching for pip, check whether a stdlib module already solves your problem — it will be faster to import, always compatible with your Python version, and trusted by millions of users.

A production Python project typically relies on a small set of tools:

  • venv — creates isolated virtual environments so dependencies do not bleed across projects.
  • pip — installs third-party packages from PyPI. Should only be used after confirming the stdlib cannot help.
  • pyproject.toml — the modern, PEP-517-standardised file for declaring package metadata, build backends, and tool configuration (replacing the old setup.py + setup.cfg approach).
  • pytest — the de-facto test runner for Python projects, building on top of the stdlib unittest infrastructure with a friendlier API.

You will explore unittest and pytest in later lessons in this chapter. For now, the key habit is: reach for the stdlib first.

A quick taste: three stdlib modules in action

Section titled “A quick taste: three stdlib modules in action”

The demo below uses three modules you will encounter constantly:

  • collections.Counter — counts hashable objects in a single pass.
  • itertools.islice — lazily slices any iterator without materialising it first.
  • json — serialises Python objects to JSON strings and deserialises them back.
import collections
import itertools
import json
# Count word frequencies
words = "the quick brown fox jumps over the lazy dog the fox".split()
counter = collections.Counter(words)
print("Top 3 words:", counter.most_common(3))
# First 5 squares using itertools
squares = list(itertools.islice((x*x for x in range(1, 100)), 5))
print("First 5 squares:", squares)
# JSON round-trip
payload = json.dumps({"lang": "Python", "batteries": True})
data = json.loads(payload)
print("Parsed:", data["lang"], "batteries:", data["batteries"])
What does 'batteries included' mean for Python?
Which module provides Counter and defaultdict?
What is the modern tool for declaring package metadata and build settings?