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
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 installrequired.
How it works step by step
- Identify the task you need to accomplish — reading a file, parsing a date, generating a UUID.
- Find the right module by browsing the Python docs or using your IDE's autocomplete.
- Import the module with
import module_name(orfrom module_name import somethingif you only need one function). - Use the functions/classes — read the docstring (e.g.,
help(json.loads)) or check the docs. - Handle errors with standard exceptions like
FileNotFoundError,json.JSONDecodeError, orValueError.
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
pathlibfor files,datetimefor dates, andsubprocessfor commands. They are the modern, recommended interfaces.
Troubleshooting & edge cases
Common mistakes and how to fix them
-
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. -
json.decoder.JSONDecodeError- Happens when reading invalid JSON — e.g., single quotes instead of double quotes, or extra commas. - Usejson.loads()with atry...exceptblock to catch and log the error. -
Path does not exist -
Path.open()oropen()raisesFileNotFoundError. - Always check withpath.exists()or wrap intry...except. -
Random isn't really random - The
randommodule uses a pseudo-random generator, fine for most apps but not secure (cryptographic). - For secure tokens, usesecretsmodule instead ofrandom.
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:
pathlibfor modern, cross-platform file handlingcsvfor CSV parsingjsonfor JSON datarandomandstringfor random generationdatetimefor date arithmeticsubprocess(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 installanything, 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 whensubprocess.run()is safer and more flexible. - Forgetting to specify encoding when opening text files, leading to
UnicodeDecodeErroron non-UTF-8 files. - Using
randomfor security-critical applications (passwords, tokens) — usesecretsinstead. - Mixing
os.pathwithpathlibkeeps code inconsistent — stick with one approach per project.
Variations
- Instead of
json.load(), you can usejson.loads()for strings andjson.dump()/json.dumps()for writing. - For more advanced path operations,
pathlib.Pathoffers methods likeglob(),rename(), andmkdir()thatos.pathlacks. - When working with dates in UTC, use
datetime.datetime.now(datetime.timezone.utc)or thezoneinfomodule (Python 3.9+).
Real-world use cases
- A web scraper uses
urllib.requestto fetch HTML andrefor pattern matching, all from the standard library. - A data pipeline reads CSV logs with
csv.DictReader, filters rows with date comparisons usingdatetime, and exports to JSON withjson.dump(). - A system administrator writes a script that uses
subprocess.run()to run shell commands andpathlibto 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 installneeded. - Use
pathlibfor modern, cross-platform file path handling instead ofos.path. - For reading/writing JSON, use
json.load()andjson.dump(); for CSV, usecsv.reader()orcsv.DictReader(). - Generate random data with
randommodule, but usesecretsfor security-sensitive tasks. - Work with dates and times using
datetime— it's intuitive and supports timezone-aware operations withzoneinfo. - Always specify file encoding (
utf-8) explicitly to avoid platform-dependent errors.
Keep learning
Related tutorials, quizzes, and articles for this topic.
Discussion
Questions, corrections, and tips help everyone reading this page.
0 comments
Add a comment
No comments yet — start the thread.