Hi, is there an easy way to use `.env` files to lo...
# prefect-community
m
Hi, is there an easy way to use
.env
files to load secrets from
os.environ
after
prefect
module import time? I want to use a
PrefectSecret
instead of an
EnvVarSecret
in my code, and I don’t want to hack the code between Prefect/EnvVar for local dev. Local context secrets should work well for overriding the PrefectSecret values, but it’s not working the way I expect. My debugger is telling me the context and secrets are set once, during
import prefect
, which means the secrets are fixed before I can load a dotenv file using my library of choice. The following pseudocode doesn’t work:
Copy code
import prefect
import environs  # .env support. .env not loaded yet.

with Flow() as flow:
  PrefectSecret("MY_SECRET")

if __name__ == "__main__":
  # For local testing
  env = environs.Env()
  env.read_env(".env")  # Load my custom PREFECT__CONTEXT__SECRETS into os.environ
  flow.run()  # Ignores new os.environ
1
k
That is right. For it to be in context, you need it to happen before Prefect is imported. So you could move it above, but I guess you lose ability to change which env. For production, maybe script based storage might help, but I don’t think this is an easy pattern to work with