Start here ยท How-to

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:

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.

ChapterFiles you create
1hello.py, args.py, version.py, tip.py
2initials.py, receipt.py
3budget_check.py, primes.py
4commands.py
5money.py โ†ป, report.py
6clean.py, ledger.py
7summary.py, budget.py
8mathx.py, use_mathx.py, money.py (now a module + script), the first tracker/ package โ†ป
9storage.py, csv_to_json.py
10errors.py, validate_all.py
11models.py, budget_oop.py
12read_stream.py
13cli.py, monthly.py
14categories.py, money_safe.py
15.venv/, test_money.py, pyproject.toml
16the 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:

  1. Run commands from the playground/ folder, which is the folder that contains your modules and packages.
  2. Run a package with -m, using dots, not slashes: python3 -m tracker, not python3 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:

ToolCommand (from playground/)Chapter
doctests in docstringspython3 -m doctest -v money_safe.py13
unittestpython3 -m unittest -v (finds test_*.py)13
pytest (in the venv)python -m pip install pytest then python -m pytest -q15

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

Troubleshooting

SymptomLikely causeFix
No playground/ folder on your machineYou are reading the book online, or downloaded only the bookgit clone https://github.com/coder-hop/python-book-playground.git playground
python3: command not foundPython isn't installed or isn't on PATHInstall it (Ch. 1); see Appendix B on PATH
SyntaxError on match, lazy import or [*x for x in โ€ฆ]Python older than 3.15Check with python3 --version; activate the venv
ModuleNotFoundError: No module named 'money'The command ran from another foldercd into playground/ first
ImportError: attempted relative import โ€ฆYou ran a package file by its pathUse python3 -m package.module from the parent folder
IndentationError / TabErrorMixed tabs and spaces, or a missing indent after :Use 4 spaces; in VS Code: Convert Indentation to Spaces
FileNotFoundError for a data fileA relative path resolved against a different working directoryRun from playground/, or build the path from Path(__file__).parent
pip: "externally-managed-environment"Installing into the system PythonActivate .venv first (Ch. 15)
An edit doesn't take effect in the REPLModules are imported only once per sessionRestart 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 with python3 -i โ†’ compare with the solution.
  • Always run from playground/, and run packages with python3 -m name.
  • Test with doctest, unittest or pytest, and delete caches and data files freely.

Official docs