kedro.framework.session.session.KedroSession

class kedro.framework.session.session.KedroSession(session_id, package_name=None, project_path=None, save_on_close=False, conf_source=None)[source]

KedroSession is the object that is responsible for managing the lifecycle of a Kedro run. Use KedroSession.create() as a context manager to construct a new KedroSession with session data provided (see the example below).

Example:

from kedro.framework.session import KedroSession
from kedro.framework.startup import bootstrap_project
from pathlib import Path

# If you are creating a session outside of a Kedro project (i.e. not using
# `kedro run` or `kedro jupyter`), you need to run `bootstrap_project` to
# let Kedro find your configuration.
bootstrap_project(Path("<project_root>"))
with KedroSession.create() as session:
    session.run()

Attributes

store

Return a copy of internal store.

Methods

close()

Close the current session and save its store to disk if save_on_close attribute is True.

create([project_path, save_on_close, env, ...])

Create a new instance of KedroSession with the session data.

load_context()

An instance of the project context.

run([pipeline_name, tags, runner, ...])

Runs the pipeline with a specified runner.

close()[source]

Close the current session and save its store to disk if save_on_close attribute is True.

Return type:

None

classmethod create(project_path=None, save_on_close=True, env=None, extra_params=None, conf_source=None)[source]

Create a new instance of KedroSession with the session data.

Parameters:
  • project_path (Path | str | None) – Path to the project root directory. Default is current working directory Path.cwd().

  • save_on_close (bool) – Whether or not to save the session when it’s closed.

  • conf_source (str | None) – Path to a directory containing configuration

  • env (str | None) – Environment for the KedroContext.

  • extra_params (dict[str, Any] | None) – Optional dictionary containing extra project parameters for underlying KedroContext. If specified, will update (and therefore take precedence over) the parameters retrieved from the project configuration.

Return type:

KedroSession

Returns:

A new KedroSession instance.

load_context()[source]

An instance of the project context.

Return type:

KedroContext

run(pipeline_name=None, tags=None, runner=None, node_names=None, from_nodes=None, to_nodes=None, from_inputs=None, to_outputs=None, load_versions=None, namespace=None)[source]

Runs the pipeline with a specified runner.

Parameters:
  • pipeline_name (str | None) – Name of the pipeline that is being run.

  • tags (Iterable[str] | None) – An optional list of node tags which should be used to filter the nodes of the Pipeline. If specified, only the nodes containing any of these tags will be run.

  • runner (AbstractRunner | None) – An optional parameter specifying the runner that you want to run the pipeline with.

  • node_names (Iterable[str] | None) – An optional list of node names which should be used to filter the nodes of the Pipeline. If specified, only the nodes with these names will be run.

  • from_nodes (Iterable[str] | None) – An optional list of node names which should be used as a starting point of the new Pipeline.

  • to_nodes (Iterable[str] | None) – An optional list of node names which should be used as an end point of the new Pipeline.

  • from_inputs (Iterable[str] | None) – An optional list of input datasets which should be used as a starting point of the new Pipeline.

  • to_outputs (Iterable[str] | None) – An optional list of output datasets which should be used as an end point of the new Pipeline.

  • load_versions (dict[str, str] | None) – An optional flag to specify a particular dataset version timestamp to load.

  • namespace (str | None) – The namespace of the nodes that is being run.

Raises:
  • ValueError – If the named or __default__ pipeline is not defined by register_pipelines.

  • Exception – Any uncaught exception during the run will be re-raised after being passed to on_pipeline_error hook.

  • KedroSessionError – If more than one run is attempted to be executed during a single session.

Return type:

dict[str, Any]

Returns:

Any node outputs that cannot be processed by the DataCatalog. These are returned in a dictionary, where the keys are defined by the node outputs.

property store: dict[str, Any]

Return a copy of internal store.

Return type:

dict[str, Any]