Build a src layout and run it with python -m
The program creates the whole project on disk (mkdir(parents=True, exist_ok=True) makes a folder and any missing parents, and rglob("*") lists everything below the current folder; Topic 7.6 teaches pathlib), lists the files, then does what an editable install achieves by putting src on the import path. runpy.run_module then runs the package exactly like python -m stockbox.
A real project has a predictable shape: code in src/<package>/, tests in tests/, a pyproject.toml, a README, a .gitignore, and settings and secrets kept out of the code. It runs with python -m package or an installed command.
Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.
Ready for more? This phase has 5 hands-on exercises with automatic checks.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Write the lines of a .gitignore file that keep these out of git: the .venv folder, the __pycache__ folders with the .pyc files inside, and the .env file.
Write code that reads the environment variable PORT, turns it into an integer, and uses 8000 when PORT is not set. Print the number.
Write the full content of a __main__.py file, in the package stockbox, that imports main from the module cli of the same package (use an absolute import) and calls it.
Explain it without notes
Describe the src/ layout and why it is preferred.
What is the difference between python script.py and python -m package, and what is __main__.py?
How should configuration and secrets be handled in a Python project?
What should be in .gitignore for a Python project, and why?
Expected output
.gitignore
README.md
src/stockbox/__init__.py
src/stockbox/__main__.py
src/stockbox/cli.py
src/stockbox/inventory.py
tests/test_inventory.py
--- python -m stockbox ---
low stock: pens, tape