Theory
ResultDesk outgrows one file
ResultDesk's Python side begins tonight, and the first temptation appears immediately: you need a square root for a statistics formula. Write your own square-root function?
Someone already did, tested it for twenty years, and shipped it inside Python: math.sqrt.
The mechanism that hands you their work, and lets you split your own growing project into tidy files, is the module. One concept, and suddenly Python stops being one long script.
Theory
Toolboxes on a shelf
A workshop does not pile every tool on one table. Tools live in labelled toolboxes: the electrical box, the plumbing box.
A module is a toolbox: math holds sqrt and pi, random holds randint, sqlite3 holds everything database. import math lifts the box onto your bench; math.sqrt opens it and takes one tool. Your own .py files are toolboxes too, the moment another file imports them.
Theory
Module, formally
A module is a file containing Python definitions and statements: functions, classes, variables. The filename minus .py is the module's name.
Why modularity earns exam marks:
- Reusability: write once, import everywhere.
- Organization: ResultDesk splits into db.py, reports.py, charts.py.
- Namespace separation: math.sqrt and your own sqrt can coexist, each behind its module's name.
Python's standard library is hundreds of pre-installed modules: math, random, datetime, csv, sqlite3.
Practical
The three import forms (plus the one to avoid)
# Form 1: import the whole toolbox, use with the prefix
import math
print(math.sqrt(49)) # 7.0
# Form 2: take one tool out, use it bare
from math import sqrt
print(sqrt(49)) # 7.0 (no math. prefix now)
# Form 3: alias a long name
import sqlite3 as sq
conn = sq.connect('college.db')
# Your own module: save this as grades.py ...
# def grade(score):
# return 'Pass' if score >= 40 else 'Fail'
# ... then in another file in the same folder:
# import grades
# print(grades.grade(78)) # Pass
# Avoid: from math import * (floods your namespace)
This example runs in Gri-Learn on the web, where you can edit it and see the output.
Quiz
A file runs: from math import sqrt. Which call is correct afterwards?
- sqrt(25): the name was imported bare, math.sqrt would need import math
- math.sqrt(25): from-import still requires the prefix
- Both sqrt(25) and math.sqrt(25) always work
- Neither: from-import only works inside functions
Show the answer
sqrt(25): the name was imported bare, math.sqrt would need import math
from math import sqrt copies the single name sqrt into your file's namespace: call it bare. The name math itself was never imported, so math.sqrt raises NameError, killing option B and C. Matching the import form to the call syntax is the most-tested module fact: import m gives m.f(); from m import f gives f().
Think first
The shadowing mystery
A student saves their Unit 4 practice file as csv.py, and inside it writes import csv to use Python's CSV toolbox. Every run crashes with AttributeError: module 'csv' has no attribute 'reader'. Before tapping: what is Python actually importing?
Show the answer
Their own file. Python searches the current folder FIRST, finds csv.py (the student's), and imports it instead of the standard library's csv: which of course has no reader inside. The file has shadowed the real module.
Fix: rename the file (csv_practice.py). Rule: never name your file after a module you import. This bug costs beginners hours precisely because the code looks perfect.
Watch out
Two habits that prevent pain
Skip star imports: from math import * dumps dozens of names into your file; two modules later, names collide and debugging is archaeology. Import what you name.
Module code runs on import, once: any print at a module's top level fires the first time someone imports it (then it is cached). Keep modules to definitions; keep actions in the importing script.
Theory
This unit is one long import
Everything ahead in this subject is a module wearing a different hat: sqlite3 (next lesson: connecting to college.db from Python), csv (Unit 4), pandas and numpy (aliased by universal convention as pd and np), matplotlib.pyplot as plt (Unit 5). Master the three import forms today and every remaining lesson starts one line faster.
Summary
Key takeaways
- A module = a .py file of definitions; the filename is the module name.
- import m → call m.f(); from m import f → call f(); import m as alias → alias.f().
- Avoid from m import *: namespace flooding and collisions.
- The standard library pre-installs math, random, datetime, csv, sqlite3 and hundreds more.
- Your own .py files import the same way, from the same folder.
- Never name a file after a module you import: it shadows the real one.
- Memory hook: toolboxes on a shelf.