- A minimal KFP component that trains a DecisionTree on the Iris dataset.
- How to run the component locally using Docker.
- The common failure when required packages are missing and how to fix it with
packages_to_install. - A fully working script and expected runtime behavior.
Dataset overview
- The Iris dataset contains feature vectors (
iris.data) and class labels (iris.target). - In this example we train a
DecisionTreeClassifierand predict the class for a single sample.
Minimal KFP component (initial example)
This minimal component uses thepython:3.10-slim base image. Many minimal Python images do not include scientific libraries such as scikit-learn, so this example may fail unless scikit-learn is available in the image.
Run the component locally using Docker
During development it’s convenient to run KFP components locally. Usekfp.local with DockerRunner() so each component runs in its own Docker container.
What happens when a required package is missing
If the base image doesn’t includescikit-learn, the component container will fail with a ModuleNotFoundError. Typical error lines:
Cause
Thepython:3.10-slim image is intentionally minimal and does not ship with many ML libraries.
Fix
The@dsl.component decorator accepts a packages_to_install argument. KFP will run pip install for the listed packages inside the component container at startup. Use packages_to_install to install scikit-learn (or any other required packages) before your component code executes.
Use
packages_to_install when your component imports libraries that are not included in the base image. This runs pip install inside the component container at startup.Corrected full script (working version)
Below is the complete working script that initializes the local Docker runner and installsscikit-learn inside the component container before execution.
Expected runtime behavior
When you run the corrected script:- KFP pulls the
python:3.10-slimbase image (if not already present). - KFP installs
scikit-learninside the container (via pip). - The component code runs and prints the prediction.
- The component returns the result as an output parameter.
Quick reference
Summary
- Define components with
@dsl.component(base_image=...). - If your component imports packages not present in the base image, add them to
packages_to_install=[...]. - Use
kfp.localplusDockerRunner()to run and test components locally before deploying to a cluster.