Maintenance

Site is under maintenance — quizzes are still available.

Go to quizzes

Python Standard Library Basics

Explore the Python standard library basics step by step. Learn core modules, apply them in a hands-on exercise, and connect to the next lesson in the Python track.

Focus: Python standard library basics

Sponsored

Ever found yourself writing a CSV parser from scratch, only to realize Python already has a battle-tested one? Or struggling to manipulate dates, generate random numbers, or access files — when the solution is already in the modules you installed with Python? That's the pain this lesson solves: knowing what's in the Python standard library so you stop reinventing wheels and write cleaner, faster, more maintainable code.

The problem this lesson solves

When you start building real applications, you'll need to read files, work with dates, generate random data, or communicate over networks. Beginners often Google around, install third-party packages, or write everything from scratch. The Python standard library — the collection of modules that ship with every Python installation — already covers files, data formats, math, dates, random values, system commands, and much more. Not knowing it leads to redundant code, security risks, and slower development.

Core concept / mental model

Think of the standard library as a giant built-in toolbox that comes free with Python. You don't need to pip install anything for common tasks like:

  • Reading and writing files → os, pathlib, io
  • Handling dates and times → datetime
  • Generating random numbers → random
  • Parsing JSON → json
  • Working with operating system paths → pathlib, os.path
  • Sending HTTP requests → urllib.request, http.client
  • Running system commands → subprocess

Each module is a namespace — a collection of related functions and classes. You import them with import module_name and use them like module_name.function().

💡 Pro tip: The standard library is always available. On any Python installation (even inside a virtual environment), these modules are present. No internet, no pip install required.

How it works step by step

  1. Identify the task you need to accomplish — reading a file, parsing a date, generating a UUID.
  2. Find the right module by browsing the Python docs or using your IDE's autocomplete.
  3. Import the module with import module_name (or from module_name import something if you only need one function).
  4. Use the functions/classes — read the docstring (e.g., help(json.loads)) or check the docs.
  5. Handle errors with standard exceptions like FileNotFoundError, json.JSONDecodeError, or ValueError.

A concrete example: reading a JSON file

# Step 1: task = read JSON config
# Step 2: module = json
import json
from pathlib import Path

# Step 3: import
config_path = Path("settings.json")

# Step 4: use
with config_path.open("r") as f:
    config = json.load(f)

print(config.get("theme", "dark"))  # 'dark'

Hands-on walkthrough

Let's practice three common standard library tasks: reading a file with pathlib, generating random data, and working with dates.

1. Read a CSV file using csv module

import csv
from pathlib import Path

# Create a sample CSV
Path("employees.csv").write_text("name,role,salary\nAlice,Engineer,80000\nBob,Designer,65000\n")

# Read it back
with open("employees.csv", newline="") as f:
    reader = csv.DictReader(f)
    for row in reader:
        print(f"{row['name']} is a {row['role']} earning ${row['salary']}")

# Output:
# Alice is a Engineer earning $80000
# Bob is a Designer earning $65000

2. Generate random passwords using random and string

import random
import string

def generate_password(length: int = 12) -> str:
    charset = string.ascii_letters + string.digits + string.punctuation
    return ''.join(random.choice(charset) for _ in range(length))

print("Random password:", generate_password())
# Example output: 'aB3!zQ9$xY2@'

3. Calculate days until a target date using datetime

from datetime import date

today = date.today()
target = date(2025, 12, 31)
delta = target - today
print(f"Days until 2025-12-31: {delta.days}")
# Example output: Days until 2025-12-31: 123

Compare options / when to choose what

Task Classic Approach (os, time) Modern Approach (pathlib, datetime) Recommendation
File path manipulation os.path.join(), os.path.dirname() Path.cwd() / "subdir", path.parent Use pathlib for readability and cross-platform safety.
Date formatting time.strftime() datetime.strftime() Use datetime for most tasks; time for timestamps.
Running system commands os.system() subprocess.run() Always prefer subprocess — it's safer and more powerful.
Reading JSON json.load() json.load() (same) json is the only option; combined with pathlib for paths.

💡 Pro tip: When in doubt, start with pathlib for files, datetime for dates, and subprocess for commands. They are the modern, recommended interfaces.

Troubleshooting & edge cases

Common mistakes and how to fix them

  1. ImportError: No module named 'something' - You might be trying to import a third-party module instead of a standard one. Standard modules never require pip install. - Check the official module index.

  2. json.decoder.JSONDecodeError - Happens when reading invalid JSON — e.g., single quotes instead of double quotes, or extra commas. - Use json.loads() with a try...except block to catch and log the error.

  3. Path does not exist - Path.open() or open() raises FileNotFoundError. - Always check with path.exists() or wrap in try...except.

  4. Random isn't really random - The random module uses a pseudo-random generator, fine for most apps but not secure (cryptographic). - For secure tokens, use secrets module instead of random.

Edge case: File encoding

On Windows, text files might be opened with the wrong encoding. Always specify encoding explicitly:

with open("data.txt", "r", encoding="utf-8") as f:
    content = f.read()

What you learned & what's next

You now understand the Python standard library's core concept — a pre-built toolbox that saves you time. You applied:

  • pathlib for modern, cross-platform file handling
  • csv for CSV parsing
  • json for JSON data
  • random and string for random generation
  • datetime for date arithmetic
  • subprocess (mentioned) for running system commands

What's next: In the next lesson, you'll build on this foundation by learning how to install and use third-party packages with pip — extending the standard library with powerful tools like requests, numpy, and flask.

📘 Key takeaways: The standard library is always available, documented, and battle-tested. Before you pip install anything, check if the standard library already has what you need.

Practice recap

Try this mini exercise: Write a script that reads a students.json file (containing a list of objects with name and score), calculates the average score, and prints a summary. Use pathlib, json, and statistics (another standard library module). Then modify it to export the result as a CSV using csv.writer.

Common mistakes

  • Assuming a module is not available and installing a third-party package unnecessarily — always check the standard library first.
  • Using os.system() for running commands when subprocess.run() is safer and more flexible.
  • Forgetting to specify encoding when opening text files, leading to UnicodeDecodeError on non-UTF-8 files.
  • Using random for security-critical applications (passwords, tokens) — use secrets instead.
  • Mixing os.path with pathlib keeps code inconsistent — stick with one approach per project.

Variations

  1. Instead of json.load(), you can use json.loads() for strings and json.dump()/json.dumps() for writing.
  2. For more advanced path operations, pathlib.Path offers methods like glob(), rename(), and mkdir() that os.path lacks.
  3. When working with dates in UTC, use datetime.datetime.now(datetime.timezone.utc) or the zoneinfo module (Python 3.9+).

Real-world use cases

  • A web scraper uses urllib.request to fetch HTML and re for pattern matching, all from the standard library.
  • A data pipeline reads CSV logs with csv.DictReader, filters rows with date comparisons using datetime, and exports to JSON with json.dump().
  • A system administrator writes a script that uses subprocess.run() to run shell commands and pathlib to manage logs across platforms.

Key takeaways

  • The Python standard library provides modules for files, data formats, math, dates, random values, and system commands — no pip install needed.
  • Use pathlib for modern, cross-platform file path handling instead of os.path.
  • For reading/writing JSON, use json.load() and json.dump(); for CSV, use csv.reader() or csv.DictReader().
  • Generate random data with random module, but use secrets for security-sensitive tasks.
  • Work with dates and times using datetime — it's intuitive and supports timezone-aware operations with zoneinfo.
  • Always specify file encoding (utf-8) explicitly to avoid platform-dependent errors.

Sponsored

Sponsored

Discussion

Questions, corrections, and tips help everyone reading this page.

0 comments

Add a comment

Shown publicly with your comment.

Be constructive · max 4,000 characters

No comments yet — start the thread.

Related tutorials, quizzes, and articles for this topic.