<@ULVA73B9P> How do I set and read a Variable from...
# ask-marvin
s
@Marvin How do I set and read a Variable from Python?
m
thought for 103.0 seconds
Here's how to set and read Variables in Python using Prefect 3.x:
Copy code
python
from prefect.variables import Variable

# Set a variable
Variable.create(name="my_variable", value="my_value")

# Get a variable
my_var = Variable.get("my_variable")
Key points: 1. Variables are meant for non-sensitive configuration data. For sensitive information, use Secret blocks instead. 2. Variable names are limited to 255 characters, and values are limited to 5000 characters. 3. Values must be JSON-serializable. 4. If a variable doesn't exist when you try to get it, a
ValueError
will be raised. You can also use the optional
default
parameter when getting a variable to provide a fallback value if the variable doesn't exist:
Copy code
python
# Get with default value
my_var = Variable.get("my_variable", default="fallback_value")
For more information, you can refer to: - Variables Concept Documentation - Variables API Reference