Hi community :slightly_smiling_face: I'm new and I...
# ask-community
y
Hi community 🙂 I'm new and I need some help for a very simple problem. I'm testing prefect since a few days, and I try to pass environment variables to a flow. It seems easy but it doesn't work for me. I have configured the flow like this flow.run_config = LocalRun(env={"GREETING": "Hello"}). And when I call flow.run, os.environ doesn't contain the GREETING env. Do you know why ? It is for local test and I don't want to register the flow right now. Thank you!
k
Hi @Yohann! Can I see your flow code?
y
Hi @Kevin Kho I tried the following code like in the documentation (https://docs.prefect.io/orchestration/tutorial/flow_config.html#configure-environment-variables) but it prints None.
from prefect import task, Task, Flow
from prefect.run_configs import LocalRun
import os
@task
def test_task():
print(os.environ.get("GREETING"))
return {}
with Flow('My Functional Flow') as flow:
flow.run_config = LocalRun(env={"GREETING": "Hello"})
res = test_task()
flow.run()
k
You’re right in that the environment does not get passed for
flow.run()
. You’d need to start an agent and start the flow from cloud/server to get this to work
y
Thank you for your answer 🙂 But how would I test a flow if I cannot pass environment ? It's still not very clear for me. I develop with a TDD approach, and I was very interested by how prefect helps to test all tasks of a flow. What would be the correct way to test a task that requires environment variable to access a BDD or anything else?
k
This is a pain point we’re aware of and we have something we’re working on that would make this process easier. In the meantime, you’d probably need to set the environment variable where you’re testing so that it can be pulled. Or you could do a simple test on a LocalAgent.
y
I understand, thank you. And well done for prefect, it is a very promising software.