Python Standard Libraries

Chapter Outline

Chapter 12: Python Standard Libraries

Python's standard library is one of its greatest strengths. It provides a wide array of modules that cover everything from file handling and math to networking and concurrency. These libraries help developers avoid reinventing the wheel and build powerful applications quickly.

In this chapter, you'll learn:

  • What the standard library is
  • Key modules grouped by category
  • How to use them with practical examples
  • A real-world example that combines several libraries
  • Chapter quiz

12.1 What is the Python Standard Library?

The Python Standard Library is a collection of modules and packages bundled with Python that you can import and use without installing anything separately. These are maintained by the core Python team and are highly reliable.

To use a standard library module:

python
import os
import math

12.2 Categories and Common Modules

CategoryCommon ModulesPurpose
File and Directory Accessos, os.path, shutil, pathlib, tempfile, glob, fnmatchWork with the file system: path manipulation, file operations, temp files
Data Serializationjson, pickle, marshal, csv, xml, plistlibRead/write structured data: JSON, CSV, XML, binary
Data Persistence / DBsqlite3, dbm, shelveLightweight database engines and key-value stores
Text Processingre, string, textwrap, difflib, unicodedata, html, xml.etree.ElementTreeRegular expressions, text formatting, Unicode handling, HTML/XML parsing
Numeric and Mathmath, decimal, fractions, random, statisticsBasic and advanced math, precise calculations, randomness
Date and Timedatetime, time, calendar, zoneinfoTime zones, formatting dates, timestamps
Concurrency / Parallelismthreading, multiprocessing, asyncio, queue, concurrent.futuresMulti-threading, multi-processing, async I/O
Networking and Internetsocket, http, urllib, ftplib, imaplib, smtplib, poplib, ssl, email, xmlrpcBuild clients/servers, send emails, HTTP requests, secure sockets
Web and Internet Datahtml.parser, urllib, http.client, xml, cgi, json, emailParsing and consuming internet data
System Utilitiessys, platform, getopt, argparse, logging, traceback, atexitCommand-line tools, platform info, logging, error handling
Security and Cryptographyhashlib, hmac, secrets, ssl, crypt, uuidSecure hashes, token generation, encryption
Development Toolspdb, doctest, unittest, trace, profile, timeit, cProfileDebugging, profiling, unit testing
Import/Runtimeimportlib, types, inspect, sys, builtinsDynamically import modules, inspect code during runtime
Operating System Servicesos, signal, resource, subprocess, shutilInterface with OS, signals, process spawning
Collections / Containerscollections, heapq, array, bisect, queue, weakref, typesExtended containers (e.g., deque, defaultdict), heaps, memory-safe refs
Compression / Archivingzipfile, tarfile, gzip, bz2, lzma, shutilRead/write compressed and archived files
Testing and QAunittest, doctest, test, pdb, trace, coverage (3rd-party)Writing and running tests
Code Compilation/Executioncompile, eval, exec, code, ast, tokenize, disDynamic code execution, parsing, AST analysis
GUI ProgrammingtkinterBuild desktop apps using the Tk GUI toolkit
Internationalizationgettext, locale, calendarLocalization, translations, culture-specific formatting
Miscellaneousuuid, base64, time, warnings, copy, contextlib, dataclasses, enum, functools, itertoolsUtilities for general programming and Pythonic idioms

12.3 Example usage

Let's now review the usage of some of these standard libraries. In later chapters we'll review some of these libraries in further details to understand additional concepts and features.

File and Directory Management

ModuleDescription
osFile paths, directory management, environment variables
shutilFile operations (copy, move, delete)
pathlibObject-oriented path handling
tempfileTemporary file creation
python
import os
from pathlib import Path
# Current directory
print(os.getcwd())
# Create a new directory
Path("new_folder").mkdir(exist_ok=True)
# List files
print(os.listdir("."))

Data Serialization

ModuleFormatUse
jsonJSONWeb data, APIs
csvCSVTabular data
pickleBinaryObject serialization
yamlYAMLRequires PyYAML (3rd party)
python
import json
data = {"name": "Alice", "age": 30}
with open("user.json", "w") as f:
json.dump(data, f)

Data Structures and Algorithms

ModulePurpose
collectionsExtra data types like deque, Counter
heapqHeap queue (priority queue)
bisectBinary search
python
from collections import Counter
names = ["Alice", "Bob", "Alice"]
print(Counter(names)) # {'Alice': 2, 'Bob': 1}

Date and Time

ModuleDescription
datetimeDate and time manipulation
timeTimestamps, delays
calendarCalendar-based calculations
python
from datetime import datetime
now = datetime.now()
print(now.strftime("%Y-%m-%d %H:%M"))

Math and Statistics

ModuleFunctionality
mathAdvanced math functions
statisticsMean, median, mode
randomRandom number generation
python
import random
print(random.randint(1, 10))

Networking and APIs

ModulePurpose
http.clientLow-level HTTP client
urllibURL handling and requests
socketTCP/IP networking
python
from urllib.request import urlopen
with urlopen("https://www.example.com") as res:
print(res.read().decode())

Testing and Debugging

ModulePurpose
unittestUnit testing framework
doctestTest examples in docstrings
pdbPython debugger
loggingApp event tracking
python
import logging
logging.basicConfig(level=logging.INFO)
logging.info("App started")

Concurrency and Parallelism

ModulePurpose
threadingLightweight parallel tasks
multiprocessingCPU-bound parallelism
asyncioAsync IO and cooperative multitasking
python
import asyncio
async def say_hello():
await asyncio.sleep(1)
print("Hello async!")
asyncio.run(say_hello())

12.4 Real-World Example: Log Archiver Utility

This utility reads logs from a directory, compresses them, and archives them into a timestamped folder.

Directory Structure Before & After

bash
logs/
├── app.log
├── errors.log
└── debug.log
archive/
└── logs_2023-08-05_14-30-00/
├── app.log.gz
├── errors.log.gz
└── debug.log.gz

The Script

python
1import os
2import gzip
3import shutil
4from datetime import datetime
5from pathlib import Path
6
7
8def compress_file(source_path: Path, dest_path: Path):
9 """Compress a log file using gzip."""
10 with open(source_path, 'rb') as f_in:
11 with gzip.open(dest_path, 'wb') as f_out:
12 shutil.copyfileobj(f_in, f_out)
13
14
15def archive_logs(log_dir: str = "logs", archive_root: str = "archive"):
16 log_dir_path = Path(log_dir)
17 archive_root_path = Path(archive_root)
18
19 if not log_dir_path.exists() or not log_dir_path.is_dir():
20 print(f"Log directory '{log_dir}' not found.")
21 return
22
23 timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
24 archive_dir = archive_root_path / f"logs_{timestamp}"
25 archive_dir.mkdir(parents=True, exist_ok=True)
26
27 log_files = list(log_dir_path.glob("*.log"))
28 if not log_files:
29 print("No .log files found.")
30 return
31
32 for log_file in log_files:
33 compressed_file = archive_dir / f"{log_file.stem}.log.gz"
34 compress_file(log_file, compressed_file)
35 print(f"Archived: {log_file}{compressed_file}")
36
37 print(f"All logs archived to: {archive_dir}")
38
39
40if __name__ == "__main__":
41 archive_logs()

12.5 Summary

You’ve learned how Python’s standard libraries provide built-in solutions for:

  • File handling
  • Dates and times
  • Data serialization
  • HTTP requests
  • Concurrency
  • Logging and testing

You should delve deeper into the various libraries and study the various functions that are provided. Mastering the standard Python libraries would allow you to build applications paster with fewer dependencies.

What is next?

In the next chapter we'll be studying Concurrency and Process Management in Python, using namely the multiprocessing and subprocess modules.

Check your understanding

Test your knowledge of Python Standard Libraries

Feedback