Sub topic 2
A Repository Layout That Scales
This chapter assumes you can write Python modules and functions, use the command line, and use basic Git (commit, branch, .gitignore). It also assumes you have trained at least one model with a library such as scikit-learn or PyTorch. The previous chapter argued that the goal is reproducible results. This chapter turns that goal into a folder structure.
What a layout has to do
A project that begins as one notebook and one CSV file works for a day. After a few weeks it has five notebooks named final_v2, a data file nobody remembers editing, and learning rates typed into cells. A layout prevents this by giving each kind of thing one home. Three kinds of things change at different speeds and for different reasons:
- Code changes when you fix a bug or add a feature. Git tracks it well.
- Configuration changes every time you try an idea: learning rate, batch size, file paths, seed.
- Data changes when new records arrive or cleaning rules change. It is often too large for Git.
If these are mixed, changing one risks breaking the others. If they are separated, you can rerun the same code with different configuration, or rerun the same configuration on a new data version, and know which part changed.
The layout
Here is a layout that works for most single-model projects and grows to several models without being rearranged.
my_project/
├── README.md
├── pyproject.toml
├── configs/
│ └── train.yaml
├── data/
│ ├── raw/
│ ├── interim/
│ └── processed/
├── models/
├── notebooks/
├── src/
│ └── my_project/
│ ├── __init__.py
│ ├── config.py
│ ├── data/
│ │ └── prepare.py
│ └── train.py
└── tests/
└── test_config.pyEach top-level entry has one job.
| Path | Holds | Tracked in Git? |
|---|---|---|
| `README.md` | What the project is, how to install it, how to run it | Yes |
| `pyproject.toml` | Package metadata and dependencies | Yes |
| `configs/` | YAML files with values that change between runs | Yes |
| `data/` | Raw, interim, and processed data, one folder per stage | No (contents), see below |
| `models/` | Trained model files and other outputs | No (contents) |
| `notebooks/` | Exploration and reporting | Yes, with care |
| `src/my_project/` | Importable code | Yes |
| `tests/` | Automated tests for the code | Yes |
Later chapters cover the contents of each folder in detail. Here we look at the structure as a whole and at the decisions behind it.
Why `src/` and a package name
Putting code in src/my_project/ rather than at the repository root can look like extra nesting. It solves a specific problem. When a package sits at the root, Python can import it just because your working directory is the root. Your tests then pass on your machine even if the package is not installable. With the src/ layout, the code is importable only after you install it, so installation problems appear early.
To make the package installable, declare it in pyproject.toml:
[build-system]
requires = ["setuptools>=68"]
build-backend = "setuptools.build_meta"
[project]
name = "my_project"
version = "0.1.0"
requires-python = ">=3.10"
dependencies = ["numpy", "pandas", "pyyaml"]
[tool.setuptools.packages.find]
where = ["src"]Then install it in editable mode from the repository root:
python -m venv .venv
source .venv/bin/activate
pip install -e .An editable install links the installed name my_project to your source folder. Edits take effect immediately, and import my_project works from any directory, including from notebooks/ and tests/. This is what lets notebooks and tests share the same code instead of copying it.
How the pieces depend on each other
Dependencies should flow in one direction. The package does not import from notebooks. The package does not contain data. Configs name data paths, but the code reads them from the config instead of embedding them.
Reading the diagram: src/ is the center. Configs and data go in, models come out, and notebooks and tests are consumers that import it. If you find yourself importing a notebook from the package, or hard-coding /home/me/Downloads/train.csv in train.py, an arrow has been reversed.
The `data/` folders
The three subfolders are stages, ordered by how much processing the data has had:
raw/holds data exactly as you received it. Never edit it by hand. Treat it as read-only.interim/holds intermediate results, such as joined tables or cleaned text, that later steps consume.processed/holds the final, model-ready tables.
The point of the stages is that every file in interim/ and processed/ can be deleted and rebuilt from raw/ by running code. If a file cannot be rebuilt that way, something was edited by hand, and the project is no longer reproducible. A later chapter covers how to version these folders without committing them to Git.
Keeping large files out of Git
Git stores every version of every committed file forever. A 2 GB dataset committed twice makes the repository very large, and removing it later requires rewriting history. Ignore the contents of data/ and models/ from the start:
data/*/*
!data/*/.gitkeep
models/*
!models/.gitkeep
.venv/
__pycache__/
.ipynb_checkpoints/The .gitkeep exceptions let you commit an empty placeholder file in each folder. Git does not track empty directories, so without a placeholder a fresh clone would not have data/raw/ at all, and scripts that write there would fail.
The role of the other folders
configs/holds files such astrain.yaml. Keeping them as plain files in one folder means a config change shows up as a normal Git diff, and you can name a run by the config file it used.tests/mirrors the structure ofsrc/my_project/. A test forsrc/my_project/config.pylives intests/test_config.py. This makes missing tests easy to spot.notebooks/is for exploration and for reporting results. Logic that you want to reuse moves intosrc/and the notebook imports it. A later chapter covers this move in detail.models/receives trained outputs. It is ignored by Git for the same reason asdata/.
Common errors
- Code that works only from one directory. Scripts that open
data/raw/x.csvbreak when run fromnotebooks/. Take paths from the config and run entry points as modules from the repository root, for examplepython -m my_project.train. - Forgetting
__init__.py. Without it, a subfolder such asdata/inside the package may not be picked up as a package by setuptools'finddirective, and imports fail after installation. Add an empty__init__.pyto each package folder. - Name clash with the top-level
data/folder. The package has its ownmy_project/data/subpackage for data-handling code, while the rootdata/holds files. They are different things. If the similar names confuse your team, rename the subpackage todatasets/orprep/. - Several experiments in one folder. If two models need different pipelines, add
src/my_project/models_a/andmodels_b/and one config per model. Do not copy the whole repository. - Committing
.venv/. Environments are rebuilt from the dependency list, not committed. Pinning that list is a later chapter's topic.
When the layout needs to grow
The layout scales by adding folders at the same level, not by deepening the existing ones. Common additions are scripts/ for one-off command-line tools, docs/ for longer documentation, and reports/ for generated figures and tables. A project with several deployable services may become a monorepo with one src/ per service, but start with a single package until the need is real. A flat structure that everyone can navigate is better than a deep one built for an imagined future.
Exercise
A teammate clones your repository, runs python -m my_project.train, and gets ModuleNotFoundError: No module named 'my_project'. Your own run works. Name the most likely cause and the fix. Then say why using the src/ layout makes this mistake easier to catch than a package at the repository root.
Show answer
The package was never installed in the teammate's environment. Fix it by creating a virtual environment and running pip install -e . from the repository root. With the src/ layout, Python cannot find my_project just because the current directory is the root, so any missing installation fails immediately on every machine. With the package at the root, the current directory puts it on the import path, so tests and scripts pass without installation, and the installation problem stays hidden until someone else, or a deployment, tries to use the package.
Exercise
You have a file data/processed/train.csv that you fixed by hand in a spreadsheet to remove some bad rows. What is wrong with this under the layout in this chapter, and what should you do instead?
Show answer
Files in processed/ should be rebuildable from raw/ by running code. A hand edit cannot be reproduced, so a rerun from raw data produces a different file. Put the cleaning rule in code, for example in src/my_project/data/prepare.py, run it to regenerate processed/train.csv, and keep raw/ untouched.