Modules and Imports
Use code from elsewhere
In this lesson
Explain it like I’m 5
Importing is borrowing a ready-made toolbox. Instead of building a hammer from scratch, you say ‘give me the hammer’ and start using it.
What a module is and how to import it
import math
print(math.sqrt(16))
print(math.floor(3.7))
4.0 3
The forms of import
import math brings in the whole module (use math.pi). from math import sqrt brings in just one name (use sqrt directly). import datetime as dt imports under a shorter alias. Prefer plain import module or from module import name; avoid from module import *, which dumps everything and hides where names came from.
import math
from random import randint
print(math.sqrt(144))
print(math.pi)
print("A dice roll between 1 and 6:", randint(1, 6))
12.0 3.141592653589793 A dice roll between 1 and 6: 4
Import the math module and print the area of a circle with radius 5 (area = π × r²). Round it to 2 decimals.
import math
radius = 5
# area = pi * radius squared
area = ___
print(round(area, 2))
Use math.pi for π and radius ** 2 for the square: math.pi * radius ** 2.
import math
radius = 5
area = math.pi * radius ** 2
print(round(area, 2))
78.54
The standard library: batteries included
Python comes with dozens of ready-made modules: math for maths, random for randomness, datetime for dates and times, json for JSON data, os and pathlib for files and folders. Before writing something tricky from scratch, check whether the standard library already has it.
from datetime import date
launch = date(2026, 1, 1)
today = date(2026, 6, 17)
days_open = (today - launch).days
print("Days since launch:", days_open)
Days since launch: 167
Importing your own files, and pip at a glance
If you have a file helpers.py next to your script, import helpers lets you use helpers.my_function(). Beyond the standard library, the wider community publishes packages you install with pip (covered later) — but the import statement is the same once they’re installed.
Explore any module with dir() and help()
You don’t have to memorise what a module offers — Python will tell you. dir(module) lists the names inside it, and help(thing) prints its documentation, including any docstrings.
These shine in the interactive REPL: import something unfamiliar, run dir() to see what’s available, then help() on whatever looks useful. It’s the fastest way to learn a new module without leaving Python.
import math
print([name for name in dir(math) if name.startswith("s")])
help(math.sqrt)
['sin', 'sinh', 'sqrt']
Help on built-in function sqrt in module math:
sqrt(x, /)
Return the square root of x.
Common mistake: ModuleNotFoundError
The name is misspelled, the module isn’t installed (for third-party packages), or your own file isn’t in the same folder / on the path.
Check the spelling, confirm standard-library names, install third-party packages with pip, and keep your own modules alongside the script that imports them.
Common mistake: Naming your file the same as a module you import
A file called random.py shadows the standard random module, so import random finds your file instead.
Avoid naming your scripts after standard modules (math.py, json.py, random.py, etc.).
What is a module?
A module is a .py file of reusable code that you bring in with import.
After import math, how do you call its sqrt function?
With import math you access its contents through the math. prefix.
Mini exercise (easy)
Use the datetime module to print today’s date in YYYY-MM-DD form.
Do it here. Finish the code below and press Run to try your answer right in the browser — no setup needed.
from datetime import date
today = None # TODO: today’s date
print(today)
from datetime import date, then date.today() — printing it already uses ISO format.
from datetime import date
today = date.today()
print(today)
from datetime import date
assert today == date.today(), "use date.today()"
print("✓ Imported and used datetime!")