Hi, I’m getting `Context object has no attribute '...
# prefect-community
j
Hi, I’m getting
Context object has no attribute 'today'
error when I try to get
prefect.context.today
in the Flow block. According to the documentation,
today
is one of those variables always available during task runs. What am I missing?
c
Are you referencing this attribute outside of a task? If so, remember that the Flow block builds your Flow but does not run it. This attribute is only populated in the context of a run
j
This is what I actually wanted to do.
Copy code
with Flow("prem_trend") as flow:

    task = RenameFlowRunTask()

    task(flow_run_id=prefect.context.flow_run_id), flow_run_name=f'run_{helpers.pstime()}')
Trying to pick up
flow_run_id
and replace the run name for it.
I get
Context object has no attribute flow_run_id
.
c
Yes, these variables are not populated until runtime but you are using them at build time in your example. To use a non-prefect specific example from your code:
Copy code
flow_run_name=f'run_{helpers.pstime()}'
This code is called once and the string is hardened when you first build this flow, it will not change each time you run the flow. If you want something dynamic, you should place this logic within your tasks or create new tasks that run this at runtime. To be very precise in your example, I would do:
Copy code
class MyRenameTask(RenameFlowRunTask):
    def run(self):
        return super.run(flow_run_id=prefect.context.flow_run_id), flow_run_name=f'run_{helpers.pstime()}')
j
Wow.. thanks. Let me try.
👍 1
sorry, it’s complaining
super
has no attribute
run
c
sorry, that should have been:
Copy code
return super().run(flow_run_id=prefect.context.flow_run_id), flow_run_name=f'run_{helpers.pstime()}')
note the parentheses
j
cool… it’s working 🙂