Variables are named, mutable string values, much like environment variables. Variables are scoped to a Prefect server instance or a single workspace in Prefect Cloud.
https://docs.prefect.io/latest/concepts/variables/
Parameters:
Name
Type
Description
Default
name
A string identifying the variable.
required
value
A string that is the value of the variable.
required
tags
An optional list of strings to associate with the variable.
classVariable(VariableRequest):""" Variables are named, mutable string values, much like environment variables. Variables are scoped to a Prefect server instance or a single workspace in Prefect Cloud. https://docs.prefect.io/latest/concepts/variables/ Arguments: name: A string identifying the variable. value: A string that is the value of the variable. tags: An optional list of strings to associate with the variable. """@classmethod@sync_compatibleasyncdefset(cls,name:str,value:str,tags:Optional[List[str]]=None,overwrite:bool=False,)->Optional[str]:""" Sets a new variable. If one exists with the same name, user must pass `overwrite=True` ``` from prefect.variables import Variable @flow def my_flow(): var = Variable.set(name="my_var",value="test_value", tags=["hi", "there"], overwrite=True) ``` or ``` from prefect.variables import Variable @flow async def my_flow(): var = await Variable.set(name="my_var",value="test_value", tags=["hi", "there"], overwrite=True) ``` """client,_=get_or_create_client()variable=awaitclient.read_variable_by_name(name)var_dict={"name":name,"value":value}var_dict["tags"]=tagsor[]ifvariable:ifnotoverwrite:raiseValueError("You are attempting to save a variable with a name that is already in use. If you would like to overwrite the values that are saved, then call .set with `overwrite=True`.")var=VariableUpdateRequest(**var_dict)awaitclient.update_variable(variable=var)variable=awaitclient.read_variable_by_name(name)else:var=VariableRequest(**var_dict)variable=awaitclient.create_variable(variable=var)returnvariableifvariableelseNone@classmethod@sync_compatibleasyncdefget(cls,name:str,default:Optional[str]=None)->Optional[str]:""" Get a variable by name. If doesn't exist return the default. ``` from prefect.variables import Variable @flow def my_flow(): var = Variable.get("my_var") ``` or ``` from prefect.variables import Variable @flow async def my_flow(): var = await Variable.get("my_var") ``` """client,_=get_or_create_client()variable=awaitclient.read_variable_by_name(name)returnvariableifvariableelsedefault
Get a variable by name. If doesn't exist return the default.
from prefect.variables import Variable
@flow
def my_flow():
var = Variable.get("my_var")
or
from prefect.variables import Variable
@flow
async def my_flow():
var = await Variable.get("my_var")
Source code in src/prefect/variables.py
666768697071727374757677787980818283848586878889
@classmethod@sync_compatibleasyncdefget(cls,name:str,default:Optional[str]=None)->Optional[str]:""" Get a variable by name. If doesn't exist return the default. ``` from prefect.variables import Variable @flow def my_flow(): var = Variable.get("my_var") ``` or ``` from prefect.variables import Variable @flow async def my_flow(): var = await Variable.get("my_var") ``` """client,_=get_or_create_client()variable=awaitclient.read_variable_by_name(name)returnvariableifvariableelsedefault
@classmethod@sync_compatibleasyncdefset(cls,name:str,value:str,tags:Optional[List[str]]=None,overwrite:bool=False,)->Optional[str]:""" Sets a new variable. If one exists with the same name, user must pass `overwrite=True` ``` from prefect.variables import Variable @flow def my_flow(): var = Variable.set(name="my_var",value="test_value", tags=["hi", "there"], overwrite=True) ``` or ``` from prefect.variables import Variable @flow async def my_flow(): var = await Variable.set(name="my_var",value="test_value", tags=["hi", "there"], overwrite=True) ``` """client,_=get_or_create_client()variable=awaitclient.read_variable_by_name(name)var_dict={"name":name,"value":value}var_dict["tags"]=tagsor[]ifvariable:ifnotoverwrite:raiseValueError("You are attempting to save a variable with a name that is already in use. If you would like to overwrite the values that are saved, then call .set with `overwrite=True`.")var=VariableUpdateRequest(**var_dict)awaitclient.update_variable(variable=var)variable=awaitclient.read_variable_by_name(name)else:var=VariableRequest(**var_dict)variable=awaitclient.create_variable(variable=var)returnvariableifvariableelseNone
@deprecated_callable(start_date="Apr 2024")@sync_compatibleasyncdefget(name:str,default:Optional[str]=None)->Optional[str]:""" Get a variable by name. If doesn't exist return the default. ``` from prefect import variables @flow def my_flow(): var = variables.get("my_var") ``` or ``` from prefect import variables @flow async def my_flow(): var = await variables.get("my_var") ``` """variable=awaitVariable.get(name)returnvariable.valueifvariableelsedefault