Huw Ringer
11/16/2021, 10:55 AMprefect.context['my_run_parameter']
in my Python code results in the following runtime error:
Failed to load and execute Flow's environment: KeyError('my_run_parameter',)
I’ve tried lots of variations on this syntax e.g.
prefect.context.get('my_run_parameter')
but nothing seems to work. Any advice/assistance you can provide would be gratefully received, thanks!Anna Geller
prefect.context['parameters'].get("your_parameter_name")
Anna Geller
Huw Ringer
11/16/2021, 12:00 PMHuw Ringer
11/16/2021, 12:27 PMFailed to load and execute Flow's environment: KeyError('parameters',)
Huw Ringer
11/16/2021, 12:37 PMAnna Geller
from prefect import Flow, Parameter, task
import prefect
@task(log_stdout=True)
def hello_world(user_input: str):
print(prefect.context["parameters"].get("user_input"))
print(f"hello {user_input}")
with Flow("test-flow") as flow:
param = Parameter("user_input", default="world")
hw = hello_world(param)
if __name__ == "__main__":
flow.run()
Anna Geller
if __name__ == "__main__":
flow.run(parameters=dict(user_input="Huw"))
Huw Ringer
11/16/2021, 12:52 PMAnna Geller
Anna Geller
Huw Ringer
11/16/2021, 2:31 PM[2021-11-16 12:55:27+0000] INFO - prefect.FlowRunner | Beginning Flow run for 'test-flow'
[2021-11-16 12:55:27+0000] INFO - prefect.TaskRunner | Task 'user_input': Starting task run...
[2021-11-16 12:55:27+0000] INFO - prefect.TaskRunner | Task 'user_input': Finished task run for task with final state: 'Success'
[2021-11-16 12:55:27+0000] INFO - prefect.TaskRunner | Task 'hello_world': Starting task run...
[2021-11-16 12:55:27+0000] INFO - prefect.TaskRunner | world
[2021-11-16 12:55:27+0000] INFO - prefect.TaskRunner | hello world
[2021-11-16 12:55:27+0000] INFO - prefect.TaskRunner | Task 'hello_world': Finished task run for task with final state: 'Success'
[2021-11-16 12:55:27+0000] INFO - prefect.FlowRunner | Flow run SUCCESS: all reference tasks succeeded
Although it works when run programmatically on a local agent, note I’m not invoking my flow using code (i.e. flow.run) - I’m invoking it specifically by clicking on the Run button in Prefect Cloud, then clicking on Show Advanced Run Configuration, then entering the key:value pair in the Context box (not user input), and it’s this Context dictionary value that I seem unable to somehow fetch via the APIHuw Ringer
11/16/2021, 2:41 PMAnna Geller
Anna Geller
from prefect import Flow, Parameter, task
import prefect
@task(log_stdout=True)
def hello_world(user_input: str):
print(prefect.context["parameters"].get("user_input"))
print(f"hello {user_input}")
with Flow("test-flow") as flow:
param = Parameter("user_input", default="world")
hw = hello_world(param)
then register:
prefect register --project YOUR_PROJECT -p testflow.py
then the above command returns a flow uuid you can use to run the flow:
prefect run --id UUID-FROM-OUTPUT-ABOVE --param user_input=Huw --watch