Code Safari

Chapter 145·Intermediate·8 min read

Modules and the Standard Library

How to use code you didn't write — importing modules, Python's huge built-in standard library, installing third-party packages with pip, and keeping projects clean with virtual environments. The chapter that connects your basics to the whole Python ecosystem.

July 27, 2026

You've reached the final chapter, and you've built something real: you can store data, make decisions, loop, organise values into collections, and package logic into functions. That's a genuine foundation. But here's the secret that makes Python so productive — you rarely have to build things from scratch. An enormous amount of the code you'll ever need already exists, written and tested by others, waiting for you to simply use it. This chapter is about reaching for that existing code: Python's modules, its famous standard library, and the vast ecosystem beyond. It's the bridge from "I know the basics" to "I can build almost anything."

Modules: importing ready-made tools

A module is simply a file of Python code — functions, values, tools — that someone has written for you to reuse. You bring one into your program with the import keyword, then use its contents with a dot:

import math
 
print(math.sqrt(16))     # 4.0 — square root
print(math.pi)           # 3.141592653589793

That one import math line hands you a whole toolkit of mathematical functions. The math. prefix says "look inside the math module for this" — so math.sqrt is the square-root function from math. Another everyday example:

import random
 
print(random.randint(1, 6))          # a random number 1–6, like a die
print(random.choice(["a", "b", "c"]))  # pick one at random

You didn't write sqrt or randint, and you never need to — you just call them, exactly like the functions you defined last chapter. That's the whole point: someone solved these problems already, and import lets you stand on their work.

Batteries included: the standard library

Here's what makes Python special among languages: it comes with a massive built-in collection of modules called the standard library — available the moment Python is installed, no downloading required. The philosophy even has a slogan: "batteries included." A small taste of what's already sitting there for you:

ModuleWhat it gives you
mathsquare roots, trig, constants like π
randomrandom numbers and choices
datetimedates, times, and the maths between them
jsonread and write JSON data
os / pathlibwork with files and folders
statisticsmean, median, standard deviation

Need today's date? import datetime. Need to read a JSON file? import json. These aren't add-ons — they ship with Python. Before you ever write a tricky routine yourself, it's worth a quick check: the standard library has probably already solved it, carefully and correctly.

Your own functions
Standard library (built in)
Third-party packages (pip install)
Where the code you use comes from

pip and PyPI: the rest of the world's code

The standard library is vast, but it can't contain everything. For specialised work — building websites, crunching data, training AI models — you'll want third-party packages: code written by the global Python community and shared on a giant repository called PyPI (the Python Package Index). You install them with a tool called pip, run from your terminal (not inside your Python file):

pip install requests

Once installed, a third-party package is used exactly like a built-in one — import it and go:

import requests
 
response = requests.get("https://api.github.com")
print(response.status_code)     # 200

requests (for fetching things over the web) isn't part of Python, but after one pip install it feels just as native. This is the doorway to Python's real power. The famous tools you may have heard of — pandas for data, NumPy for numbers, Django and Flask for web apps, PyTorch and TensorFlow for machine learning — are all third-party packages, a single pip install away. There are hundreds of thousands of them.

Virtual environments: keeping projects tidy

One practical habit rounds out your readiness for real work. Different projects often need different — sometimes conflicting — packages: project A wants version 1 of a library, project B needs version 2. Installing everything into one shared pile leads to clashes. The fix is a virtual environment (a "venv"): a private, isolated set of installed packages that belongs to one project.

python -m venv .venv           # create an isolated environment
source .venv/bin/activate      # switch into it (Mac/Linux)
pip install requests           # installs only inside this project

With a venv active, pip install puts packages into this project's private space, leaving your system and other projects untouched. You don't need to master this today, but knowing the concept — each project gets its own clean set of dependencies — will save you real confusion the moment you build something serious. It's standard practice on virtually every professional Python project.

Where this leaves you — and the whole guide

Take a moment to see how far you've come. You started not knowing what a variable was. You now understand the core of an entire programming language: values and types, variables, decisions and loops, collections, functions — and now how to reach beyond your own code into Python's standard library and the vast ecosystem of packages the whole world shares. That is a complete foundation, not a partial one.

What's remarkable is how little of the language you needed to become genuinely capable. Python's basics really are this approachable — and everything more advanced (classes and objects, error handling, working with files and data, building web apps or training models) is a natural extension of the ideas you now hold. The single best next step is to build something: a small script that solves a real annoyance in your day. You have every tool you need to start, and the whole import-able world to help you finish. Welcome to Python.

Modules and the Standard Library | Code Safari