Hey, I'm trying to create a remote deployment usin...
# ask-community
l
Hey, I'm trying to create a remote deployment using the following
Copy code
import os
from prefect.deployments import Deployment
from prefect.filesystems import LocalFileSystem

os.environ['PREFECT_API_URL'] = f'http://<remote_host>:4200/api'
local_file_system_block = LocalFileSystem.load('local-system')
deployment = Deployment.build_from_flow(...)
deployment.apply
However find that it doesn't seem to utilise the environ variable and instead hits the ephemeral-prefect host rather than the <remote_host> is this expected? setting this by
prefect config set PREFECT_API_URL=http://<remote_host>:4200/api
works however
n
Can you try to set the ENV variable before importing any prefect code? I think prefect does not really support changing ENV variables during runtime/after prefect imports
Copy code
import os
os.environ['PREFECT_API_URL'] = f'http://<remote_host>:4200/api'

from prefect.deployments import Deployment
from prefect.filesystems import LocalFileSystem

...
l
Indeed worked, do you know if there is a way to reload in the case that I don't want to do a late import? Otherwise can fall back to importing in my function
1
Interesting from the docs
Copy code
The SettingsContext is set when the prefect module is imported. This context is referred to as the "root" settings context for clarity. Generally, this is the only settings context that will be used. When this context is entered, we will instantiate a Settings object, loading settings from environment variables and defaults, then we will load the active profile and use it to override settings.
So the settings are loaded on import
Same docs mention
Copy code
Another SettingsContext may be entered at any time to change the settings being used by the code within the context. Generally, users should not use this. Settings management should be left to Prefect application internals.
For those following along I got around this with
Copy code
with prefect.settings.temporary_settings(updates={prefect.settings.PREFECT_API_URL: '<http://xxx.xxx.com:4200/api'}>):
    local_file_system_block = LocalFileSystem.load('local-system')
    deployment = Deployment.build_from_flow(...)
    deployment.apply
And this was the docs from the API that made it clear
Copy code
>>> from prefect.settings import PREFECT_API_URL
        >>>
        >>> with temporary_settings(updates={PREFECT_API_URL: "foo"}):
        >>>    assert PREFECT_API_URL.value() == "foo"
🙏 1
n
Oh that’s a nice find, thanks for sharing!