https://prefect.io logo
m

Marwan Sarieddine

01/21/2021, 11:08 PM
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

Zanie

01/21/2021, 11:57 PM
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

Marwan Sarieddine

01/22/2021, 12:20 AM
Hi @Zanie thanks for your prompt response - and as far as importing globally vs locally you also see no difference there right ?
z

Zanie

01/22/2021, 12:22 AM
Yep!
a

Alex Papanicolaou

01/22/2021, 1:08 AM
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

Zanie

01/22/2021, 1:39 AM
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

Alex Papanicolaou

01/22/2021, 1:44 AM
Thanks for confirming!