Python Standard Library: What Ships Without Installing Anything
In the previous post we learned to handle errors gracefully. Now let us talk about something that will save you enormous amounts of time: Python’s standard library.
The Python Standard Library is the collection of modules that ships with every Python installation, covering file system operations, dates and times, data serialisation, mathematics, networking, testing, and compression without requiring a single third-party install. The first instinct of many beginners — and honestly of many experienced developers — is to reach for a third-party package. Often the right answer is already in the box.
The Python community has a saying: batteries included. This post is about finding the batteries.
Python Modules and the import Statement
Before exploring specific modules, a quick note on how to use them. A module is a Python file — or a package of files — that you bring into your script with import:
import math
print(math.sqrt(144)) # 12.0
If you only need one thing from a module, from x import y keeps the namespace cleaner:
from math import sqrt, pi
print(sqrt(144)) # 12.0
print(pi) # 3.141592653589793
You can alias a module to a shorter name with as:
import datetime as dt
today = dt.date.today()
That is all there is to it. The rest is knowing which modules exist and what they do.
pathlib — File Paths Done Right
🔒 Subscribe to keep reading.
datetime — Dates and Times
🔒 Subscribe to keep reading.
json — Data Exchange
🔒 Subscribe to keep reading.
random — Controlled Randomness
🔒 Subscribe to keep reading.
dataclasses — Structured Data Without Ceremony
🔒 Subscribe to keep reading.
Complete Example: Combining pathlib, json, random, and dataclasses
🔒 Subscribe to keep reading.
Python Standard Library vs Third-Party Packages: When to Reach for Each
🔒 Subscribe to keep reading.
References
🔒 Subscribe to keep reading.
Subscribe to unlock the full article ❤️
I keep most of the site completely open. A few unusually detailed tutorials need a free subscriber login so I can keep publishing this kind of work.
Log in to unlock
The form below signs you up for the newsletter. It does not log you into the app — log in afterwards (same email) to unlock this article and download your subscriber gifts. New subscribers get an inbox mail: Set a password to unlock articles.
Full content temporarily unavailable — refresh in a moment