https://kedro.org/ logo
#questions
Title
# questions
a

Arnaud Dhaene

03/11/2024, 11:36 AM
Hey everyone! Quick question about hooks and the Kedro context: I'd like to set an environment variable in the instantiation of the Kedro context (in
_setup_env_variables
) and then access these environment variables in the instantiation of my custom Kedro hook, which I have added to
settings.py
with
HOOKS = (CustomHook(),)
It seems like I am getting a KeyError as the wanted environment variables are not yet set when the Hook is instantiated... Is there any way around this? TIA!
1
Additional question: does this work differently when using
KedroSession
? What are the implications?
m

Merel

03/11/2024, 4:48 PM
Hi @Arnaud Dhaene, could you give a bit more information? • How are you instantiating the
KedroContext
? • How exactly are you setting the environment variables? • What hook specifications are you using?
a

Arnaud Dhaene

03/11/2024, 5:15 PM
Hey! I ended up figuring out the following: • Hooks are instantiated before the context is loaded by the session. • This means if your Hook needs environment variables that are set during
session.load_context()
, you will need to access those after initiation (e.g.,
before_pipeline_run
and not in
__init__
m

Merel

03/11/2024, 5:20 PM
Ah yes different hooks are instantiated at different points in the process. Did you find this out through our docs (e.g. https://docs.kedro.org/en/stable/hooks/introduction.html) or by trying stuff yourself? Anything we could do better in making the process clearer?
a

Arnaud Dhaene

03/11/2024, 9:28 PM
By trying out stuff myself. It was not super clear to me what happens to hooks after you define
HOOKS = ...
in
settings.py
thankyou 1