Example (tested): two modules
prices.py:
"""Helpers for prices."""
TAX_RATE = 0.10
def with_tax(price, rate=TAX_RATE):
return round(price * (1 + rate), 2)
print("prices.py: __name__ is", __name__)
if __name__ == "__main__":
print("Running prices.py directly:", with_tax(20))
shop.py:
import prices
from prices import with_tax
print(prices.TAX_RATE, with_tax(50), with_tax(50, rate=0.05))
Run python3 prices.py: it prints prices.py: __name__ is __main__ and Running prices.py directly: 22.0. Run python3 shop.py: it prints prices.py: __name__ is prices (only once, even though shop.py imports prices twice) and 0.1 55.0 52.5. The "Running directly" line does not appear, because prices.py was imported, not run.
ClaudeStuck? Copy a ready-made prompt for Claude
Copy this, open Claude, paste it, and Claude will walk you through the lesson step by step.
For a grown-up: Claude is for adults (18+). Copy this, paste it into Claude, and use the answer to explain the lesson to your child.
I'm learning "Python Projects: testing, packaging and version control" on The Future Market. Lesson 1: "Modules, packages and a project layout".
What the lesson teaches:
- A module is any .py file. Another file can use it with import prices or from prices import with_tax.
- A module's code runs once, the first time it is imported in a program.
- Every module has a name in __name__. When you run a file directly, its __name__ is "__main__". When it is imported, __name__ is the module's name. So code inside if __name__ == "__main__": runs only when you run the file itself, not when someone imports it.
- A package is a folder of modules. The Python Packaging User Guide recommends an __init__.py file in it (it can be empty), so the folder imports as a regular package.
- The packaging tutorial uses the src layout: your package folder sits inside a folder called src, next to pyproject.toml, README.md, LICENSE and a tests folder.
- A file called __main__.py inside a package lets people run the package with python3 -m packagename.
- Good habit: keep the logic (calculations, checks) in one module with no printing and no input(), and keep the interface (argparse, printing) in another module. Logic without printing is much easier to test (Lesson 3).
The exercise I'm working on:
Add a third module labels.py to the l1 folder with a function money(x) that returns text like $12.50. Import it in shop.py and print money(with_tax(50)). Add a if __name__ == "__main__": block to labels.py that prints a quick self-check, and prove it does not run when shop.py imports it.
I'm stuck. Please:
1. Explain the main idea in plain, simple words with one everyday example.
2. Walk me through the exercise one small step at a time. Ask me what I've done so far before giving the next step.
3. Don't just give me the final answer. Help me do it myself, and check my work when I share it.
I'm a parent helping my child (about 8 years old) with a lesson called "Modules, packages and a project layout" from the course "Python Projects: testing, packaging and version control".
Here's what the lesson says:
A big program in one file is like a kitchen with every pot, spoon and plate in one drawer. Modules are separate drawers, each with one kind of thing. A package is a cupboard full of labelled drawers, and the project layout is the plan of the whole kitchen, so anyone can find anything.
The activity:
Add a third module labels.py to the l1 folder with a function money(x) that returns text like $12.50. Import it in shop.py and print money(with_tax(50)). Add a if __name__ == "__main__": block to labels.py that prints a quick self-check, and prove it does not run when shop.py imports it.
Please explain this so an 8-year-old understands: short sentences, a fun everyday example, and no hard words.
Then give me 3 simple questions I can ask my child to check they understood, and a way to do the activity together, one tiny step at a time.