I'm having some issue importing prefect.context.to...
# ask-community
j
I'm having some issue importing prefect.context.today. Was this changed from 14.16 to 14.17? I'm on .16, can't seem to use "import prefect.context" https://docs.prefect.io/api/latest/utilities/context.html
k
Hi @Joseph Loss, What error do you get?
j
Copy code
import prefect.context
Traceback (most recent call last):
  File "D:\venv\py38\.venv\lib\site-packages\IPython\core\interactiveshell.py", line 3437, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-4-9e439fa2ff82>", line 1, in <module>
    import prefect.context
  File "C:\Users\joe.loss\AppData\Local\JetBrains\Toolbox\apps\PyCharm-P\ch-0\211.7142.13\plugins\python\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
ModuleNotFoundError: No module named 'prefect.context'
k
Hey I think the docs are wrong there. You should just use
import prefect
then
prefect.context
.
j
hmm I'm still having a bit of trouble here. Essentially I'd like to make a parameter update by default to the current day. But when registering the flow, the default is just the current dt.today() - it does not update every day. I was hoping to use prefect.context() to accomplish this
import prefect prefect.context['today'] Traceback (most recent call last):
Copy code
File "D:\venv\py38\.venv\lib\site-packages\IPython\core\interactiveshell.py", line 3437, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-51-ab54c2887a26>", line 1, in <module>
    prefect.context['today']
  File "D:\venv\py38\.venv\lib\site-packages\prefect\utilities\collections.py", line 80, in __getitem__
    return self.__dict__[key]  # __dict__ expects string keys
KeyError: 'today'
does it need to be called within the task rather than the flow? If so, can I still use it as a parameter for that task?
k
In a call but will get back to you in a bit.
Yes I think you need to do it in a task. I think the trick here is to make your
Parameter(day, default="today")
, and then you make a task where you pull a date out.
j
@Kevin Kho That worked! Thank you so much
Copy code
@task(log_stdout=True, nout=2)
def fnGetPricesTiinga(tickers, assetTypes, startDate = '2015-01-01', endDate = None, freq = 'daily'):
    logger = prefect.context.get("logger")
    endDate = prefect.context.today


with Flow('daily-tiingo-loader') as flow:
    start_date = Parameter('start_date', default='2021-05-09')
    end_date = Parameter('end_date', default="today")
    fnGetPricesTiinga(...,..., end_date,...)
👍 1