Setting Up Your Environment
You can read this whole book without installing anything: every result is in the prose, every position is diagrammed, and every chart is described. But the book is built to be re-run. Every number in it comes from a script you can execute on data you can download, and the progressive project, Outpost, runs on your own games. This page gets you from nothing to a working setup.
What you need
- Python 3.13. The book's code was written and run on Python 3.13.
- Stockfish 19, the free open-source chess engine. Every engine evaluation in the book is from Stockfish 19, and the evaluation cache is keyed by engine version.
- Disk space. The processed data files take about 1.6 GB. The raw downloads they are built from are roughly another 0.6 GB (broadcast PGN files, the puzzle database, the FIDE list and the PGN Mentor collections), and the caches about 0.2 GB. The Lichess sample is streamed, so the multi-gigabyte monthly Lichess file is never stored.
- A terminal — PowerShell or Command Prompt on Windows, Terminal on macOS, any shell on Linux.
- An internet connection for the first data build. After that the book works offline: every network call goes through a cached wrapper.
Step 1 — Get the book's files
Clone the repository or download and unzip it, then open a terminal in its root folder (the one
containing book.toml, requirements.txt and the toolkit/ folder). The folders that matter for code:
chess-analytics/
├── toolkit/ the book's shared library: chessdata.py, engine.py, boards.py, structures.py, tests/
├── project/outpost/ the progressive project, one module per chapter
├── scripts/ data build, validation and book-build scripts
├── engine/ where Stockfish goes (engine/stockfish.exe on Windows)
├── data/
│ ├── raw/ downloads (input to the build scripts)
│ ├── processed/ parquet files the chapters read
│ ├── cache/ engine, tablebase, explorer and Chess.com caches
│ └── reference/ small reference tables shipped with the book
└── part-0N-…/chapter-NN-…/code/ each chapter's scripts
Step 2 — Create a virtual environment and install the libraries
python -m venv .venv
# Windows: .venv\Scripts\activate
# macOS/Linux: source .venv/bin/activate
python -m pip install --upgrade pip
python -m pip install -r requirements.txt
requirements.txt installs python-chess, pandas, NumPy, Matplotlib, seaborn, SciPy, scikit-learn,
PyArrow, zstandard and requests — and nothing else. The chapters use only those libraries. To run the
toolkit's tests you will also want pytest (python -m pip install pytest).
Step 3 — Install Stockfish 19
Download Stockfish 19 for your platform from the official site, https://stockfishchess.org/download/.
- Windows. Unzip the download, and copy (or rename) the executable to
engine\stockfish.exeinside the book's folder. The toolkit looks there by default. - macOS and Linux. Put the binary anywhere you like and tell the toolkit where it is with the
STOCKFISH_PATHenvironment variable, for exampleexport STOCKFISH_PATH="$HOME/bin/stockfish"(add it to your shell profile). Package managers sometimes ship an older Stockfish; check the version before relying on it.
Check that the toolkit can see the engine. From the book's root folder:
python -c "import sys; sys.path[:0]=['toolkit']; import engine; print(engine.engine_version()); print(engine.evaluate('rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1', depth=20)['cp'])"
It should print Stockfish 19 and then the start position's evaluation in centipawns — 30 with the
book's cache, which the chapters write as +0.30 (Stockfish 19, depth 20). A different Stockfish version
works, but it will not use the book's cached evaluations, and its numbers will differ slightly from the
ones printed in the chapters.
Step 4 — Download and build the data
All data lives under data/. If your main drive is short of space, data/raw and data/processed can
be folders (or links to folders) on another drive, or you can point the toolkit at a different data
folder with the CHESSDATA_DIR environment variable.
The build scripts are in scripts/. Each is idempotent — run it again and you get the same output from
the same input. Run them from the book's root folder.
| Data | Where it comes from | Put the download at | Build command |
|---|---|---|---|
| Lichess sample (4,000,000 games, October 2026) | Streamed directly from https://database.lichess.org/ | — (no download needed) | python scripts/build_lichess_sample.py 2026-10 4000000 |
| Broadcast (OTB) games, Nov 2025–Oct 2026 | Lichess broadcast database: https://database.lichess.org/broadcast/lichess_db_broadcast_YYYY-MM.pgn.zst, saved under the shorter name → |
data/raw/broadcast/broadcast_YYYY-MM.pgn.zst (one file per month) |
python scripts/build_broadcast.py |
| Master games | Player files at https://www.pgnmentor.com/files.html | data/raw/pgnmentor/<Player>.zip (e.g. Morphy.zip) |
python scripts/build_masters.py |
| FIDE rating list | Combined list at https://ratings.fide.com/download_lists.phtml | data/raw/fide_players_list.zip |
python scripts/build_fide.py |
| Lichess puzzles | https://database.lichess.org/#puzzles | data/raw/lichess_db_puzzle.csv.zst |
python scripts/build_puzzles.py |
| Per-game features and position sample | Built from the Lichess and broadcast files above | — | python scripts/build_features.py |
| Chess.com archives (Hikaru, GothamChess) | Chess.com Published-Data API | — | python scripts/fetch_chesscom.py |
A few notes:
- The Lichess sample streams the October 2026 monthly file and stops after the requested number of
games, so it downloads only the first part of the month. Usernames are replaced by a salted one-way
hash as the games are read. Re-running it rebuilds
lichess_games.parquetandlichess_moves.parquetfrom scratch; if you already have the book's processed files, there is no reason to. - The broadcast build reads every
broadcast_*.pgn.zstindata/raw/broadcast/, so name your monthly downloads to match. It removes duplicate relays and zero-move forfeits, and flags engine events, online events and variants. - The feature build replays every game that has move text — about 1.2 million — and is by far the
slowest step: it uses every CPU core and writes resumable shards to
data/processed/_feature_shards/. If it is interrupted, run it again and it skips the finished shards;python scripts/build_features.py assemblerebuilds the outputs from existing shards. - Chess.com.
fetch_chesscom.pyalso asks for Magnus Carlsen's archive, which the public API does not provide; that request fails and the script carries on. - Your numbers will drift if your downloads differ. The FIDE list and the Chess.com archives change every month. To reproduce the book exactly, use the snapshot described in About the Data; with newer downloads the methods still work, and the numbers will be close but not identical.
To check the build, run the toolkit's tests (27 of them, covering the metric definitions and the processed data):
python -m pytest -q toolkit/tests
The caches
Engine analysis is the slow part of chess analytics, so the toolkit caches everything that touches an engine or the network:
| Cache | Contents |
|---|---|
data/cache/engine.sqlite |
Every Stockfish evaluation, keyed by position (FEN), depth, number of lines and engine version |
data/cache/tablebase.sqlite |
Syzygy tablebase answers (≤ 7 pieces) from the Lichess tablebase server |
data/cache/explorer/ |
Lichess masters-explorer answers, each stored with its retrieval date |
data/cache/chesscom/ |
The two Chess.com public archives |
With a warm cache, re-running a chapter takes seconds; a cold run recomputes evaluations and can differ
from the printed numbers by a few centipawns. Two practical points: the Lichess games explorer
(as opposed to the masters explorer) needs a personal Lichess API token, set as LICHESS_TOKEN, and the
book doesn't use it — it computes the same statistics from its own Lichess sample. And Outpost's
import command reads only from caches unless you pass --online.
Running the chapter scripts
Each chapter's scripts run from that chapter's folder:
cd part-01-strategic-foundations/chapter-02-pawn-structure
python code/01_pawn_anatomy.py
Every script starts by putting
toolkit/ and project/ on Python's path, so no installation of the book's own code is needed. To run
every script in one or more chapters and get a pass/fail list:
python scripts/run_all_code.py # every chapter
python scripts/run_all_code.py 14 15 16 # selected chapters
Outpost runs from the project/ folder:
cd project
python -m outpost import --persona dev --work runs/dev
python -m outpost analyze --work runs/dev
python -m outpost report --work runs/dev
Building the book
The book builds two ways from the same Markdown sources.
python scripts/gen_summary.py # regenerate SUMMARY.md (mdBook) and _toc.yml (Jupyter Book)
python scripts/build_mdbook.py # mdBook edition into ./book (requires mdBook installed)
jupyter-book build . # Jupyter Book edition (uses _config.yml and _toc.yml)
python scripts/compile_single_file.py # one-file Markdown edition for print or offline reading
python scripts/validate_book.py # structure, links, figures and content checks
build_mdbook.py stages a copy of the sources and converts the maths into the form mdBook's MathJax
expects before building, so run it rather than mdbook build directly. mdBook is a separate install
(https://rust-lang.github.io/mdBook/); Jupyter Book is a Python package. The _toc.yml format used
here is Jupyter Book 1's.
Troubleshooting
| Symptom | Cause and fix |
|---|---|
UnicodeEncodeError when printing (Windows) |
The console isn't using UTF-8. The book's scripts reconfigure output themselves; in your own scripts add sys.stdout.reconfigure(encoding="utf-8"), or set PYTHONIOENCODING=utf-8. |
A FileNotFoundError (after a few retries) when the engine starts |
Stockfish isn't at engine/stockfish.exe. Move it there, or set STOCKFISH_PATH to the binary's full path. |
| Parallel analysis hangs or re-runs the script on Windows | Code that calls engine.analyse_games_parallel must sit under if __name__ == "__main__": — Windows starts worker processes by re-importing your script. |
| Engine numbers differ slightly from the book | A different Stockfish version, a cold cache, or a different hash state. A few centipawns is normal; check the depth first. |
| Comparisons on flag columns behave oddly when reading parquet directly | Read through the loaders (chessdata.game_features(), chessdata.lichess_games() and the rest). Reading the files with pandas directly leaves some flags as strings and missing values as pd.NA. |
| Explorer request returns 401 | You queried the Lichess games explorer, which needs a token. Use db='masters', or compute from lichess_games(). |
find_game returns the wrong game |
It returns the first match. When two players met more than once, add event=, year= or round=. |
| A broadcast Chess960 game won't replay | The processed broadcast data doesn't keep Chess960 start positions; Chapter 37's code folder caches the ones it uses. |
| A script runs for many minutes the first time | It is filling the engine cache. The second run is fast. |
If something else goes wrong, see CONTRIBUTING for how to report it.