https://prefect.io logo
Title
j

Jenia Varavva

08/01/2022, 1:16 AM
How do I programmatically assign
Settings
values? E.g. I want to set
PREFECT_API_URL
from inside the python script?
k

Khuyen Tran

08/01/2022, 1:19 AM
For now, we only support assigning
PREFECT_API_URL
from the command line. You could try to use
os.system
to run the command in your Python script
j

Jenia Varavva

08/01/2022, 1:32 AM
@Khuyen Tran thanks for the suggestion. Is this setting change expected to be picked up by the current interpreter? E.g. when running:
import os
from prefect.settings import PREFECT_API_URL, get_default_settings, Settings
os.system("prefect config set PREFECT_API_URL=<http://prefect-orion.prefect/api>")
print(PREFECT_API_URL.value())
print(get_default_settings().PREFECT_API_URL)
print(Settings().PREFECT_API_URL)
I’m seeing:
None
None
None
If I add
os.environ["PREFECT_API_URL"] = "foo"
the last line switches to the desired value, but when I run a
flow
it seems to still use the local/sqlite API
Ok, looks like using
context.use_profile
is almost what I wanted:
import prefect.context 
from prefect.settings import Profile
p = Profile(
  name="default", 
  settings={
    "PREFECT_API_URL": "<http://prefect-orion.prefect/api>"
  },
)
with prefect.context.use_profile(p):
  run_my_flow()
Now I just need a way to install this profile as the default..
k

Khuyen Tran

08/01/2022, 1:45 AM
wait actually you can assign a new value to a profile in a Python script
from prefect.settings import update_current_profile
from prefect.settings import PREFECT_API_URL

update_current_profile(settings={PREFECT_API_URL: "foo"})
assert PREFECT_API_URL.value() == "foo"
j

Jenia Varavva

08/01/2022, 1:47 AM
The assertion fails for me for some reason
i.e.
value()
is
None
While
with prefect.context.use_profile(p):
    print(PREFECT_API_URL.value())
shows the expected value
k

Khuyen Tran

08/01/2022, 1:49 AM
that is weird. It doesn’t fail for me
which version of Prefect are you in?
j

Jenia Varavva

08/01/2022, 1:50 AM
2.0.1
now it works, hm.. something with my environment. Let me see
k

Khuyen Tran

08/02/2022, 3:14 PM
Is it still working for you?
j

Jenia Varavva

08/02/2022, 4:17 PM
Thanks, yes. It worked for me, but I have concluded my experiments with 2.0 for now, deeming it slightly too raw for my use case. In particular, https://prefect-community.slack.com/archives/CL09KU1K7/p1659334343783899 (/me trying to get attention for that thread here)