Hi folks, I have a very general question/clarifica...
# prefect-community
m
Hi folks, I have a very general question/clarification concerning the importing and handling of the prefect
context
object. Is there a difference between importing the
context
globally vs from within a task ? i.e.
Copy code
import prefect

@task
def t():
    prefect.context.get("...")
vs
Copy code
@task
def t():
    import prefect
    prefect.context.get("...")
Also in a good portion of the docs the
context
is used as
prefect.context
- is that purely for readability reasons or is there a side effect for using
from prefect import context
that one should be aware of ? (My current intuition/understanding is that there shouldn't be any difference here)
z
Hi @Marwan Sarieddine — there shouldn’t be a difference here. I think it’s probably safer to
import prefect
instead of doing
from prefect import context
but I would have to read more about how python populates the module at import-time to know for sure.
I do not think I’ve ever seen an issue from using
from prefect import context
m
Hi @Zanie thanks for your prompt response - and as far as importing globally vs locally you also see no difference there right ?
z
Yep!
a
Hi @Zanie This topic came up in a discussion between @Marwan Sarieddine and me and I could have sworn I had read that one had to be careful with the context object. I’m trying to find it and perhaps this is issue is close. It’s obviously very old. https://github.com/PrefectHQ/prefect/issues/1710
z
Ah yeah — it is possible that you’ll run into pickling problems. It seems like the error message should be clear if that happens though.
👍 1
a
Thanks for confirming!