Skip to content
Tutorials Center
My learning

Topics

Lesson 18 of 41 ยท Model Packaging and Registry

Packaging and Environments

Learn why a model needs its exact environment, how to pin and lock dependencies, how to capture package versions, and how to detect environment mismatches before deployment.

"It works on my machine" is the most expensive sentence in MLOps. A trained model depends on more than its weights: the Python version, scikit-learn or PyTorch version, NumPy version and even the operating system can change its behavior. Packaging means bundling the model with a precise description of the environment it needs, so production can recreate it.

What belongs in a model package

A model package

Model file The serialized pipeline.
Metadata Inputs, outputs, lineage.
Dependencies Exact library versions.
Entry point A predict function or service.

Everything a stranger needs to run it.

1 / 2

Capture the environment

At training time, record what is actually installed. In a real project you use pip freeze or a lock tool; here we read versions from the running interpreter.

Python Snapshot installed versions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The == pins match exactly the training environment. Loose ranges such as scikit-learn>=1.0 let a different version in tomorrow and break loading (see pickle version coupling).

Direct versus transitive dependencies

You list a few direct dependencies; each pulls in dozens of transitive ones. A lock file (uv.lock, poetry.lock, pip-compile output) pins all of them, with hashes, so two installs a month apart are identical. Keep two files:

File Contains Purpose
pyproject.toml / requirements.in Direct dependencies with sensible ranges What humans edit
uv.lock / requirements.txt (compiled) Every package pinned exactly What machines install

Check for a mismatch at start-up

A service should compare its environment with the one recorded by training and refuse to start on a critical mismatch.

Python Detect an environment mismatch

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A patch-level difference (3.12.1 to 3.12.3) is usually a warning; a minor or major difference in a library that defines your model is an error.

An entry point: the model's public face

Package a small function so callers never touch internals:

Python
# predictor.py
import joblib, pandas as pd

_model = joblib.load("model.joblib")

def predict(records: list[dict]) -> list[float]:
    frame = pd.DataFrame(records)
    return _model.predict_proba(frame)[:, 1].tolist()

The service (see building a prediction service) calls only predict. Swapping the model or its internals does not change callers.

Common packaging forms:

  • A Python wheel with the model file included as package data.
  • An MLflow model directory (MLmodel file, environment spec, artifacts).
  • A container image, covered in the next lesson.

Rebuild the environment from scratch in CI and run the model's tests there. An environment that has never been rebuilt is a guess.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why pin library versions exactly for a deployed model?

    Libraries change numerics, defaults and internals; exact pins recreate the training behavior.
  2. 2. What does a lock file add over a list of direct dependencies?

    A lock file records the full resolved dependency set, usually with hashes.
  3. 3. What should a service do if a critical library version differs from training?

    A mismatch can silently change predictions, so failing fast is safer.
  4. 4. What is the purpose of an entry-point function such as predict()?

    A stable entry point lets the model or its internals change without changing callers.

Summary

  • A model package = model file + metadata + exact dependencies + an entry point.
  • Capture the training environment and pin it; use lock files for transitive dependencies.
  • Compare serving and training environments at start-up and fail loudly on critical differences.
  • Containers freeze the whole environment, system libraries included.

Next: Containerizing a Model