Using the Python Playground
The playground/ folder is your workbench: you type every example and exercise there, run it, break it, and fix it. This page shows you how to set it up once and how to work in it every day.
You will learn
- What the playground is and what it already contains.
- How to open it in an editor and a terminal, and set up a virtual environment.
- The everyday loop: create a file, run it, experiment in the REPL, fix it.
- Which chapter adds which files, and how they grow into the Chapter 16 project.
- How to run tests, compare with the reference solution, and fix common problems.
What is the playground?
It's a plain folder, published as its own repository on GitHub: coder-hop/python-book-playground. Nothing in it is magic: it's just where your practice files live. Exercises marked playground in each chapter tell you which file to create there.
๐ก Rule of thumb: one file per exercise, named exactly as the exercise says. Later chapters import earlier files (for example, Chapter 15's tests import money.py), so matching names matters.
One-time setup (10 minutes)
1. Get the playground
Clone the repository (or use Code โ Download ZIP on the repository page):
The final playground argument names the folder playground/, so every path in this book (playground/hello.py, playground/reference/) matches your copy exactly. Put it next to the book or anywhere you like.
2. Check Python 3.15
If you see an older version, or "command not found", follow Chapter 1: Installing Python 3.15. With uv you can also run uv python install 3.15 and then use python3.15 wherever this book writes python3.
3. Open the folder in an editor and a terminal
Any editor works. With VS Code:
- Install the official Python extension (by Microsoft). It gives you syntax checking, autocompletion and a Run button.
- Open the built-in terminal with Ctrl+`. It starts in the playground folder, which is exactly where you want to run commands.
- Set the editor to 4 spaces for indentation (VS Code does this for
.pyfiles by default).
4. Create a virtual environment (recommended)
You won't need third-party packages until Chapter 15, but making the venv now means every command uses the same Python. (Chapter 15 explains venvs fully.)
In VS Code, run the command Python: Select Interpreter (Ctrl/Cmd+Shift+P) and pick the one in .venv. New terminals then activate it automatically.
โ ๏ธ Activate it in every new terminal (or let VS Code do it). If the prompt doesn't start with (.venv), you're using the system Python.
The everyday loop
Every exercise follows the same four steps.
1. Create the file
2. Run it from the playground folder
3. Experiment in the REPL
When you're unsure what something does, try it in the interactive prompt before writing it in a file. python3 -i file.py runs your file first and then leaves you in the REPL with all its variables, which is perfect for poking at your own code.
You can also import your file in a normal REPL started in the playground folder:
4. Compare with the solution, then improve
Open the exercise's Show solution only after you have something running. If yours works differently, that's fine: read the "why it works" sentence and decide which version you prefer.
๐ก Read errors from the bottom. The last line of a traceback names the problem. The line above it shows your code (Chapter 1 and Chapter 10).
What each chapter adds
Over the book, the playground fills up like this. Files marked โป are revisited and extended in a later chapter.
| Chapter | Files you create |
|---|---|
| 1 | hello.py, args.py, version.py, tip.py |
| 2 | initials.py, receipt.py |
| 3 | budget_check.py, primes.py |
| 4 | commands.py |
| 5 | money.py โป, report.py |
| 6 | clean.py, ledger.py |
| 7 | summary.py, budget.py |
| 8 | mathx.py, use_mathx.py, money.py (now a module + script), the first tracker/ package โป |
| 9 | storage.py, csv_to_json.py |
| 10 | errors.py, validate_all.py |
| 11 | models.py, budget_oop.py |
| 12 | read_stream.py |
| 13 | cli.py, monthly.py |
| 14 | categories.py, money_safe.py |
| 15 | .venv/, test_money.py, pyproject.toml |
| 16 | the full tracker/ package and test_tracker.py, replacing Chapter 8's version |
If the folder gets crowded, make one sub-folder per chapter (ch01/, ch02/, โฆ) for the single-file exercises. Keep money.py, tracker/, pyproject.toml and the tests at the top level, because they import each other.
Running packages and multi-file code
From Chapter 8 on, some files import others. Two rules prevent almost every import problem:
- Run commands from the
playground/folder, which is the folder that contains your modules and packages. - Run a package with
-m, using dots, not slashes:python3 -m tracker, notpython3 tracker/__main__.py.
โ Fails
โ Works
โ ๏ธ Never name your files after standard modules (random.py, json.py, csv.py, email.py). Python would import your file instead of the real module (Chapter 8). If it happens, rename the file and delete its __pycache__ entry.
Testing your work
Three ways to check your code, from lightest to most thorough:
| Tool | Command (from playground/) | Chapter |
|---|---|---|
| doctests in docstrings | python3 -m doctest -v money_safe.py | 13 |
| unittest | python3 -m unittest -v (finds test_*.py) | 13 |
| pytest (in the venv) | python -m pip install pytest then python -m pytest -q | 15 |
Using the reference project
playground/reference/ holds the finished Chapter 16 tracker. Use it to see the goal, to get unstuck, and to compare once you're done. Don't edit it; work in your own tracker/.
To see how your version differs from it:
Optional: track your progress with git
Committing after each chapter gives you a history to look back on, and an easy undo.
Cleaning up
__pycache__/folders are Python's caches (Chapter 8). They're safe to delete at any time:find . -name __pycache__ -prune -exec rm -rf {} +- Data files that exercises create (
expenses.json,demo.json,*.csv,*.log) can be deleted too; the scripts recreate them. - A broken venv? Delete
.venv/and create it again; it's disposable.
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
No playground/ folder on your machine | You are reading the book online, or downloaded only the book | git clone https://github.com/coder-hop/python-book-playground.git playground |
python3: command not found | Python isn't installed or isn't on PATH | Install it (Ch. 1); see Appendix B on PATH |
SyntaxError on match, lazy import or [*x for x in โฆ] | Python older than 3.15 | Check with python3 --version; activate the venv |
ModuleNotFoundError: No module named 'money' | The command ran from another folder | cd into playground/ first |
ImportError: attempted relative import โฆ | You ran a package file by its path | Use python3 -m package.module from the parent folder |
IndentationError / TabError | Mixed tabs and spaces, or a missing indent after : | Use 4 spaces; in VS Code: Convert Indentation to Spaces |
FileNotFoundError for a data file | A relative path resolved against a different working directory | Run from playground/, or build the path from Path(__file__).parent |
pip: "externally-managed-environment" | Installing into the system Python | Activate .venv first (Ch. 15) |
| An edit doesn't take effect in the REPL | Modules are imported only once per session | Restart the REPL (exit, then python3 again) |
Recap
playground/is your workbench;playground/reference/is the finished project to compare against.- Set it up once: Python 3.15, open the folder in your editor, and create and activate
.venv. - The loop: create the file the exercise names โ
python3 file.pyโ experiment withpython3 -iโ compare with the solution. - Always run from
playground/, and run packages withpython3 -m name. - Test with doctest, unittest or pytest, and delete caches and data files freely.