Phase 1: Programming Fundamentals I · ~35 minutes · Python · uv
Modules and Imports — Organizing Code
import isn't magic. It's "go run that other file, then let me reach into what it defined."
Hiring signal: Understands import as a real mechanism (run once, cache, reach into a namespace), not a black box
What you will learn
- Explain what import actually does: run a file once, then expose the names it defined
- Use at least two standard library modules (math, random, or datetime) via import
- Diagnose a real ModuleNotFoundError from a typo and a real circular import error
- Split a script that's grown too long into two cooperating files
Introduction
Type: Learn Languages: Python Prerequisites: Lesson 04 (Functions — Abstraction and Reuse) Time: ~35 minutes
Objective
Learning objectives
- Explain what
import actually does: run a file once, then expose the names it defined - Use at least two standard library modules (
math, random, datetime) via import - Diagnose a real
ModuleNotFoundError from a typo and a real circular import error - Split a script that's grown too long into two cooperating files
What you're building
Take a script that's grown too long — a to-do list manager with both the display logic and the data-processing logic tangled together in one file. Split it into:
todo_helpers.py — containing at least two functions (for example, format_task(task, done) and count_remaining(tasks)), with no top-level code that produces output when importedmain.py — that imports todo_helpers and uses its functions to process a small hardcoded list of tasks- A comment in
main.py diagnosing what specific error would occur if todo_helpers were imported as import todo_helper (missing the s) instead
Two files exist: config.py (which does import app) and app.py (which does import config, then uses config.SETTING at the top level, before config.py has defined it). What's the most accurate description of what happens?
This is exactly the mod_a/mod_b pattern from this lesson, with different names. Python doesn't detect circular imports "in advance" or retry anything — it follows imports literally, in the order they're written, and hands back a partially-loaded module rather than looping forever when it detects a module already being loaded. Whichever file tries to use a name from the other one before that other one has reached the line defining it will hit a real AttributeError, at that exact line — not before, and not as some special circular-import-specific error type.
Unlock the full lesson
You've read the first 2 sections. The rest of this lesson covers The Problem, Check Yourself, Key Terms & Next — plus a hands-on lab, quiz, and project artifact.
Create a free account to unlock Phase 0 and Phase 1 of every course — no credit card.
Browse all courses · View pricing · DeVenture Academy