Setting Up Your Environment

Everything in this book runs on free software, on an ordinary computer, offline once the data is downloaded. This page takes you from a bare machine to the point where every script in the book runs. It takes about half an hour, most of it waiting for downloads. Chapter 2 covers the same ground more slowly, with an explanation of every line of the toolkit; this page is the quick version, and the place to come back to when something breaks.

What you need

  • A computer running Windows 10 or 11, macOS, or Linux. Any laptop from the last ten years or so is enough. 8 GB of RAM is comfortable; 4 GB works if you load only the columns you need (Chapter 2, §2.6, shows how — the full men's Test deliveries table takes about 2 GB in memory).
  • Disk space: roughly 1 GB for Python and the libraries, and about 145 MB for the data (measured below).
  • An internet connection for the one-time downloads: a few hundred megabytes of libraries and about 93 MB of data. After that, nothing in the book needs the internet.
  • Python 3.10 or newer. The book's code was run with Python 3.13.

No graphics card, paid software or cloud account is needed anywhere in the book.

Step 1 — Install Python

Windows. Download the installer from python.org. On the first screen, tick "Add python.exe to PATH" before you click Install — forgetting this is the most common reason python "is not recognized" afterwards. Open a new PowerShell or Command Prompt window and check:

python --version

The installer also provides the py launcher; if python is not found, py --version usually works, and you can use py wherever this page says python.

macOS. Download the macOS installer from python.org, or use brew install python if you already use Homebrew. In Terminal the command is usually python3:

python3 --version

Linux. Python 3 is almost certainly installed (python3 --version). On Debian and Ubuntu you may also need the python3-venv and python3-pip packages from your package manager.

Alternative: Miniconda or Anaconda. If you are on a machine where you cannot install system software — a university lab computer, say — Miniconda installs everything inside your user folder. Create an environment with conda create -n cricket python=3.13, activate it with conda activate cricket, and then follow Step 4 with pip inside it. Pick one route; don't mix conda and venv in the same environment.

Step 2 — Get the book's files

The book's code and reference data come as one folder, cricket-analytics/. Download it from the book's page on DataField.Dev — as a ZIP file you unpack, or with git clone if you use git — and put it somewhere with a short path and no special characters (for example C:\cricket\cricket-analytics on Windows, or ~/cricket/cricket-analytics on macOS and Linux).

Inside you will find:

cricket-analytics/
├── toolkit/            cricdata.py, venues.py, tracking_sim.py   (the shared data layer)
├── data/
│   ├── raw/            Cricsheet zips + people.csv               (created on first run)
│   ├── processed/      <code>_deliveries.parquet, <code>_matches.parquet   (the cache)
│   └── reference/      IPL auction prices, player styles, and their sources
├── project/crease/     the analytics platform you build, one module per chapter
├── part-01-foundations/
│   └── chapter-01-cricket-moneyball-moment/
│       ├── index.md    the chapter, plus six companion files
│       ├── code/       the chapter's scripts
│       └── images/     the chapter's figures
├── ...                 parts 2–8, appendices
└── requirements.txt    the Python libraries the book uses

The data/raw/ and data/processed/ folders start empty: the book does not redistribute Cricsheet's files, so you download them yourself in Step 5.

Step 3 — Create a virtual environment

A virtual environment is a private folder of libraries for one project, so that installing something for this book can never break anything else on your machine. Open a terminal in the cricket-analytics folder and run:

Windows (PowerShell):

python -m venv .venv
.venv\Scripts\Activate.ps1

If PowerShell refuses with a message about running scripts being disabled, either run Set-ExecutionPolicy -Scope CurrentUser RemoteSigned once and try again, or use Command Prompt instead, where the activation command is .venv\Scripts\activate.bat.

macOS and Linux:

python3 -m venv .venv
source .venv/bin/activate

When the environment is active, your prompt starts with (.venv). You need to activate it again in each new terminal window before running the book's code.

Step 4 — Install the libraries

With the environment active:

python -m pip install --upgrade pip
pip install -r requirements.txt

requirements.txt lists the libraries every chapter uses — pandas, numpy, pyarrow, matplotlib, seaborn, scipy, statsmodels, scikit-learn, xgboost, plotly, networkx and lifelines — plus streamlit, which only the Chapter 39 dashboard needs. pip resolves these to about sixty packages in total, including their dependencies; on a slow connection the download is the longest part of the whole setup. Leave it running.

Check that the key libraries import:

python -c "import pandas, pyarrow, sklearn, xgboost; print(pandas.__version__)"

If that prints a version number (2.2 or later), you are ready for the data.

Step 5 — Download and build the data

The toolkit file toolkit/cricdata.py downloads Cricsheet's ball-by-ball archives and turns each one into two fast Parquet tables — one row per delivery, one row per match — which every chapter loads. It does this automatically the first time any script asks for a competition, but it is better to do it once, up front, so that later scripts start instantly.

Start with a single competition to check that everything works:

python toolkit/cricdata.py ipl
ipl: 1,243 matches, 295,732 deliveries

Then build all twenty archives the book uses. The command is long but it is one line:

python toolkit/cricdata.py tests_male odis_male t20s_male t20s_female it20s_male odis_female ipl bbl psl cpl sat hnd ilt mlc bpl lpl ntb sma wpl wbb
tests_male: 899 matches, 1,730,523 deliveries
odis_male: 2,617 matches, 1,388,641 deliveries
t20s_male: 3,649 matches, 820,658 deliveries
t20s_female: 2,200 matches, 499,978 deliveries
it20s_male: 240 matches, 52,749 deliveries
odis_female: 620 matches, 324,744 deliveries
ipl: 1,243 matches, 295,732 deliveries
bbl: 662 matches, 153,250 deliveries
psl: 357 matches, 83,799 deliveries
cpl: 445 matches, 103,717 deliveries
sat: 130 matches, 29,020 deliveries
hnd: 389 matches, 75,636 deliveries
ilt: 134 matches, 31,407 deliveries
mlc: 109 matches, 25,529 deliveries
bpl: 469 matches, 111,232 deliveries
lpl: 143 matches, 32,917 deliveries
ntb: 1,570 matches, 360,545 deliveries
sma: 695 matches, 158,839 deliveries
wpl: 88 matches, 20,656 deliveries
wbb: 527 matches, 120,569 deliveries

Those are the counts for the book's snapshot of 18 November 2026. If you download later, Cricsheet will have added matches and your counts will be higher; see About the Data for what that means for the book's printed outputs.

What to expect on disk and in time. Measured on the snapshot:

Folder Contents Size
data/raw/ 20 Cricsheet zip archives 91.6 MB
data/raw/people.csv Cricsheet's people register (downloaded when first used) 1.2 MB
data/processed/ 40 Parquet files (deliveries and matches for each archive) 50.0 MB
Total about 143 MB

The download time depends on your connection: about 93 MB takes a minute or two on a fast line and perhaps ten to fifteen minutes on a slow one. Parsing the JSON into Parquet took 93 seconds for all twenty archives on the fast laptop used to build this book; allow five minutes or more on an older machine. The men's Tests and ODIs are the largest; the Women's Premier League takes under a second. You only pay this cost once. After that, loading the full IPL takes a fraction of a second.

Check everything at once. Chapter 1 includes a script that checks your Python version, the libraries, the toolkit and all twenty data files, and compares one number with the book. From the Chapter 1 folder:

cd part-01-foundations/chapter-01-cricket-moneyball-moment
python code/project_checkpoint.py

It prints a line for each check — [OK ] or [MISSING] — ending with:

5. Sanity check against the chapter
  [OK     ] Kohli IPL runs/balls/outs    (9336, 6926, 231) (chapter says (9336, 6926, 231))

READY FOR CHAPTER 2

Running the chapter scripts

Every chapter's scripts live in its code/ folder and are numbered in the order the chapter uses them. Run them from the chapter's folder:

cd part-01-foundations/chapter-01-cricket-moneyball-moment
python code/01_scoring_revolution.py

Each script begins with a short header that finds the book's root folder from its own location and adds toolkit/ and project/ to Python's search path, so the scripts work wherever you put the book. Figures are written to the chapter's images/ folder rather than opened in windows, so scripts behave the same on a laptop or a server. Most scripts finish in seconds; the heaviest take a few minutes.

Jupyter notebooks or scripts?

The book's code is written as scripts: plain .py files run top to bottom. That is deliberate. A script always runs in order from a clean start, which is why you can reproduce every number in the book.

Notebooks are excellent for exploring — load the IPL once and poke at it for an hour. Jupyter is not in requirements.txt; if you want it, install it into the same environment:

pip install jupyterlab
jupyter lab

In a notebook, the script header's Path(__file__) does not exist. Put this in your first cell instead, adjusting the path to wherever your book folder lives:

import sys
from pathlib import Path
BOOK = Path(r"C:\cricket\cricket-analytics")      # your book folder
sys.path.insert(0, str(BOOK / "toolkit"))
sys.path.insert(0, str(BOOK / "project"))
from cricdata import load_deliveries, load_matches, batting_summary, bowling_summary

Before you trust a number from a notebook, use Restart kernel and run all so that no leftover variables from earlier experiments are hiding in it.

Windows: the rupee sign and other characters

The data and the book's output contain characters outside basic English — the rupee sign ₹, accented player names, the dashes in competition names. On Windows, some consoles use an older text encoding and a script that prints one of these characters stops with an error like:

UnicodeEncodeError: 'charmap' codec can't encode character '\u20b9' in position 0: character maps to <undefined>

The data is fine; only the console can't display the character. Tell Python to use UTF-8 for its output. In PowerShell, for the current window:

$env:PYTHONIOENCODING = "utf-8"

In Command Prompt: set PYTHONIOENCODING=utf-8. To make it permanent, add PYTHONIOENCODING with the value utf-8 to your user environment variables in Windows Settings. Alternatively, run scripts with python -X utf8 code/01_scoring_revolution.py. macOS and Linux terminals use UTF-8 already.

Troubleshooting

python is not recognized (Windows). Python is not on your PATH. Try py instead, or rerun the installer, choose Modify, and tick the PATH option. Open a new terminal afterwards.

ModuleNotFoundError: No module named 'pandas' (or any library). The virtual environment is not active in this terminal, or the install did not finish. Activate it (Step 3) and run pip install -r requirements.txt again.

An error about a missing Parquet engine, or "Unable to find a usable engine". pyarrow is missing. pip install pyarrow.

ModuleNotFoundError: No module named 'cricdata'. You ran a script from the wrong place, or moved it out of the book's folder structure. Run chapter scripts from their chapter folder as shown above, and keep toolkit/ where it is.

BadZipFile or "File is not a zip file". A download was interrupted and left a partial archive. Delete that file from data/raw/ (for example data/raw/tests_male_json.zip) and the matching Parquet files from data/processed/, then run the toolkit command again.

A download times out or fails with an HTTP error. Check your connection and try again — the toolkit skips archives it already has, so rerunning the full command only fetches what is missing. On a very slow or metered connection, download one archive at a time, starting with ipl, which most chapters use.

MemoryError, or the computer slows to a crawl when loading Tests or ODIs. The largest tables are big in memory even though they are small on disk. Read only the columns you need with pd.read_parquet(path, columns=[...]), as Chapter 2 shows, and close other programs.

My output differs slightly from the book's. Check that your data is the same snapshot (the match counts in Step 5). Different library versions can also shift the last decimal place of a model's output; differences larger than that usually mean newer data. Either way, the method is what matters.

PowerShell won't activate the environment. See the execution-policy note in Step 3.

Something else. Rerun Chapter 1's code/project_checkpoint.py: it tells you which piece is missing. Appendix B documents every toolkit function and column, and Appendix A is a Python and pandas reference.

Once the checkpoint says READY FOR CHAPTER 2, you have everything the book needs.