<@ULVA73B9P>. Can you give me an explanation of pr...
# ask-marvin
b
@Marvin. Can you give me an explanation of prefect profiles and what are they used for?
m
Prefect profiles are used to manage and store settings for your Prefect environment. They are persisted to the
PREFECT_PROFILES_PATH
, which by default is stored in your
PREFECT_HOME
directory. You can create, inspect, rename, and delete profiles using the Prefect CLI. For example, to create a new profile with no settings, you would use the command
prefect profile create test
. To inspect the settings of a profile, you would use
prefect profile inspect test
. You can also set values in the current profile or in another profile by using the
prefect config set
command. For example, to set a value in the current profile, you would use
prefect config set VAR=X
. To set a value in another profile, you would use
prefect --profile "foo" config set VAR=Y
. For more detailed information, you can refer to the Prefect documentation.
n
fwiw i most commonly use 2 profiles: • one that points at Prefect Cloud
Copy code
PREFECT_API_URL='<https://api.prefect.cloud/api/accounts/xxx/workspaces/xxx>'
• one that points at the ephemeral API (open-source server)
Copy code
» prefect profile use oss
⠋ Connecting...
Connected.

» prefect profile inspect oss
PREFECT_API_URL='<http://127.0.0.1:4200/api>'
👍 1
b
Thanks for the info! how do i switch between the profiles?
n
>
Copy code
prefect profile use oss
> 
> prefect profile use someotherprofile
👍 1
b
how can i set the API_URL and the API_KEY in python: I tried the set_() function in prefect.cli.config but it does not work -> app.console.print(message, **kwargs) AttributeError: 'PrefectTyper' object has no attribute 'console'
n
you can set the env var or use prefect config set as mentioned above, whats your use case where you need to set a setting value programmatically?
b
programatically authenticaticate with oauth2, get the auth token and set it as PREFECT_API_KEY
token_url = settings.auth_url access_token = get_access_token(token_url=token_url, client_id=settings.prefect_client_id, client_secret=settings.prefect_secret) settings_to_set = [f"PREFECT_API_URL={settings.prefect_api_url}", f"PREFECT_API_KEY={access_token}"] prefect.cli.config.set_(settings_to_set)
? @Marvin how can i set the API_URL and the API_KEY programatically in python:
m
To set the
PREFECT_API_URL
and
PREFECT_API_KEY
programmatically in Prefect 2.x with Python, you can use environment variables. Here's an example of how you can do this:
Copy code
python
import os

# Set the Prefect API URL
os.environ["PREFECT_API_URL"] = "<https://api.prefect.cloud/api/accounts/[ACCOUNT-ID]/workspaces/[WORKSPACE-ID]>"

# Set the Prefect API key
os.environ["PREFECT_API_KEY"] = "[API-KEY]"
Replace
[ACCOUNT-ID]
,
[WORKSPACE-ID]
, and
[API-KEY]
with your actual Prefect account ID, workspace ID, and API key. Please be aware that setting environment variables like this will only affect the current process and any subprocesses that it spawns. These variables will not be set globally on your system and will not persist after your Python script terminates. If you need a more persistent configuration, consider using
prefect
CLI commands or setting these variables in your environment before running your script. For more details, you can visit the Prefect documentation on connecting to Prefect Cloud: Prefect Cloud - Connecting to the API.
n
perhaps temporary_settings would be helpful here?
b
I executed the commands as subprocesses in the python script