How do I programmatically assign `Settings` values...
# ask-community
j
How do I programmatically assign
Settings
values? E.g. I want to set
PREFECT_API_URL
from inside the python script?
k
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
@Khuyen Tran thanks for the suggestion. Is this setting change expected to be picked up by the current interpreter? E.g. when running:
Copy code
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:
Copy code
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:
Copy code
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
wait actually you can assign a new value to a profile in a Python script
Copy code
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
The assertion fails for me for some reason
i.e.
value()
is
None
While
Copy code
with prefect.context.use_profile(p):
    print(PREFECT_API_URL.value())
shows the expected value
k
that is weird. It doesn’t fail for me
which version of Prefect are you in?
j
2.0.1
now it works, hm.. something with my environment. Let me see
k
Is it still working for you?
j
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)