Lifecycle management with
KedroSession allows you to:
Manage the lifecycle of a Kedro run
Persist runtime parameters with corresponding session IDs
Traceback runtime parameters, such as CLI command flags and environment variables
KedroSession decouples Kedro’s library components, managed by
KedroContext, and any session data (both static and dynamic data). As a result, Kedro components and plugins can access session data without the need to import the
KedroContext object and library components.
The main methods and properties of
create(): Create a new instance of
KedroSessionwith session data
close(): Close the current session — although we recommend that you use the session object as a context manager, which will call
close()automatically, as opposed to calling the method explicitly
run(): Run the pipeline with the arguments provided; see Running pipelines for details
Create a session¶
The following code creates a
KedroSession object as a context manager and runs a pipeline inside the context, with session data provided. The session automatically closes after exit:
from kedro.framework.session import KedroSession
from kedro.framework.startup import bootstrap_project
from pathlib import Path
with KedroSession.create() as session:
You can provide the following optional arguments in
project_path: Path to the project root directory
save_on_close: A boolean value to indicate whether or not to save the session to disk when it’s closed
env: Environment for the
extra_params: Optional dictionary containing extra project parameters for the underlying
KedroContext; if specified, this will update (and therefore take precedence over) parameters retrieved from the project configuration
configure_project handle the setup of a Kedro project, but there are subtle differences:
bootstrap_project is used for project mode, and
configure_project is used for packaged mode.
Kedro’s CLI runs the functions at startup as part of
kedro run so in most cases you don’t need to call these functions. If you want to interact with a Kedro project programatically in an interactive session such as Notebook, use
%reload_kedro line magic with Jupyter or IPython. Only use these functions directly if none of these methods work.
This function uses
configure_project, and additionally reads metadata from
pyproject.toml and adds the project root to
sys.path so the project can be imported as a Python package. It is typically used to work directly with the source code of a Kedro project.
This function reads
pipeline_registry.py and registers the configuration before Kedro’s run starts. If you have a packaged Kedro project, you only need to run
configure_project before executing your pipeline.