Rinze
07/21/2021, 2:56 PMconfig.toml
. I've tried many things already so I'd your input since I might be overlooking something.
To keep everything together, I prefer to have the config.toml
in the same git folder as my flows ( (base/src/config.toml
). In base/.env
there are a bunch of environment variables, among others PREFECT__USER_CONFIG_PATH
. Initially this worked on my local machine and the things defined in the config file were used in the flow, both on Server and when I switched to Cloud. But now I cloned the repo to a different machine and it fails to import the config.toml. Strangely enough, when I output the value of PREFECT__USER_CONFIG_PATH
to the log, it is correct. More info in the thread.
Is there a better way to do this, or am I forgetting something?Rinze
07/21/2021, 2:59 PMbase/
--.env
--src/
----api.py
----config.toml
----run_agent.py
base/.env
PREFECT__USER_CONFIG_PATH=config.toml
...
base/src/run_agent.py
import socket
from pathlib import Path
from dotenv import dotenv_values
from prefect.agent.local import LocalAgent
hosts = {
'***': r'C:\users\***\base\src'
}
hostpath = Path(hosts.get(socket.gethostname(), '.'))
env_vars = dotenv_values()
env_vars['PREFECT__USER_CONFIG_PATH'] = str(hostpath / 'config.toml')
LocalAgent(labels=['agenttest'],
env_vars=env_vars,
import_paths=paths,
show_flow_logs=True
).start()
base/src/api.py
import os
import prefect
from prefect import task, Flow
from prefect import config as pc
@task(name='Check last date of entries in database')
def check_last_date():
logger = prefect.context.get('logger')
<http://logger.info|logger.info>(f'config: {os.environ.get("PREFECT__USER_CONFIG_PATH")}')
if pc.db_to_use != 'SQLite':
...
with Flow(name='Fetch data from API',
run_config=UniversalRun(labels=['agenttest'])
) as api_flow:
last_date = check_last_date()
base/src/config.toml
db_to_use = 'MSSQL'
...
Running on Windows 10 with Python 3.9. I know officially there's no support yet for 3.9, but that can't be it right?
Also, I've reregistered the flow many times. Every time the flow fails because db_to_use
is not found in config.tomlKevin Kho
prefect.config.user_config_path
to see if that gives the same value?Kevin Kho
hosts = {
'***': r'C:\users\***\base\src'
}
I am assuming you changed that right? Since it seems you loaded the correct env variable.Kevin Kho
str(hostpath / 'config.toml')
or config.toml
?emre
07/21/2021, 3:05 PMRinze
07/21/2021, 3:06 PMprefect.config.user_config_path
, but it's the same outputRinze
07/21/2021, 3:06 PMRinze
07/21/2021, 3:07 PMRinze
07/21/2021, 3:08 PM~/.prefect/config.toml
, but that will take it out of VCSKevin Kho
config.toml
with different values and seeing if your Flow loaded the content of it?Rinze
07/21/2021, 3:11 PMFile "C:\Users\rjcdo\Documents\Werk\datafuture\code\pipeline\venv\lib\site-packages\box\box.py", line 501, in __getattr__
raise BoxKeyError(str(err)) from _exception_cause(err)
box.exceptions.BoxKeyError: "'Config' object has no attribute 'db_to_use'"
emre
07/21/2021, 3:12 PMPREFECT__USER_CONFIG_PATH
exactly once in the runtime (when importing prefect.configuration
)
https://github.com/PrefectHQ/prefect/blob/64acf232d2ffe733c975797b4144bd8237fb57e9/src/prefect/configuration.py#L14
What this means is that changing the env var value after importing prefect.configuration
, or import prefect
, has no effect, prefect has already tried to read PREFECT__USER_CONFIG_PATH
, couldn't find a value, and used the default config.tomlemre
07/21/2021, 3:14 PMenv_vars = dotenv_values()
env_vars['PREFECT__USER_CONFIG_PATH'] = str(hostpath / 'config.toml')
Rinze
07/21/2021, 3:15 PMRinze
07/21/2021, 3:17 PMload_dotenv()
at the top of the file before. Although always a bit of a mind bender, I am now familiar with the runtime conceptemre
07/21/2021, 3:17 PMRinze
07/21/2021, 3:25 PMsrc/config.toml
to ~/.prefect
. Like I said before it didn't work, but now I added some text to the logging message and reregistered. Now db_to_use
is read. I then removed ~/.prefect/config.toml
and it still works 🤯Rinze
07/21/2021, 3:25 PMKevin Kho
Rinze
07/21/2021, 3:29 PM~/.prefect
(as expected)Rinze
07/21/2021, 3:30 PMRinze
07/21/2021, 3:33 PMfrom prefect import config as pc
if pc.db_to_use etc etc
Kevin Kho
import config as pc
might be checking environment variables, but the environment variables loaded by dotenv are only in the Python process . IKevin Kho
Rinze
07/21/2021, 3:35 PMemre
07/21/2021, 3:50 PMPREFECT__USER_CONFIG_PATH
, change your run_agent.py as follows:
import socket
from pathlib import Path
from dotenv import dotenv_values
hosts = {
'***': r'C:\users\***\base\src'
}
hostpath = Path(hosts.get(socket.gethostname(), '.'))
env_vars = dotenv_values()
env_vars['PREFECT__USER_CONFIG_PATH'] = str(hostpath / 'config.toml')
from prefect.agent.local import LocalAgent
LocalAgent(labels=['agenttest'],
env_vars=env_vars,
import_paths=paths,
show_flow_logs=True
).start()
I moved the prefect import LocalAgent part below your envvar setup.emre
07/21/2021, 4:04 PMos.environ
instead of dotenv_values
helped me overwrite environment variables, this snippet seems to start the localagent with the correct PREFECT__USER_CONFIG_PATH
, but the dotenv_values version didn't have any effect. Probably dotenv_values
returns a static copy of yout .env
state, instead of the actual effective envvars.
import os
os.environ["PREFECT__USER_CONFIG_PATH"] = str("config.toml")
from prefect.agent.local import LocalAgent
LocalAgent(labels=["agenttest"]).start()
Rinze
07/21/2021, 7:56 PMPREFECT__USER_CONFIG_PATH
in os.environ + env_vars
in agent definition: no
• using load_dotenv()
(instead of dotenv_values()
) and `env_vars`: no
• dotenv_values()
and put it in `os.environ`: no
• combination of previous and PREFECT__USER_CONFIG_PATH
in `os.environ`: no
I've come to the conclusion that any time you load anything in env_vars
, the normal environment is disregarded.Rinze
07/21/2021, 8:01 PM.env
file in Cloud. Still interested if anyone finds a working solution!
SESSIONNAME: Console
PREFECT__USER_CONFIG_PATH: config.toml
PREFECT__LOGGING__LEVEL: DEBUG
GIT_TOKEN: ***
PREFECT__CONTEXT__SECRETS__***: ***
PREFECT__CONTEXT__SECRETS__***: ***
PREFECT__CONTEXT__SECRETS__***: ***
PREFECT__CONTEXT__SECRETS__***: ***
PREFECT__CONTEXT__SECRETS__***: ***
[2021-07-21 19:48:31,393] INFO - agent | Registering agent...
[2021-07-21 19:48:31,573] INFO - agent | Registration successful!
And still unable to load the config.toml