<@ULVA73B9P> Hi How in python i have option to set...
# ask-marvin
d
@Marvin Hi How in python i have option to set read delete variables
m
thought for 127.8 seconds
You can also use these CLI commands to manage variables:
Copy code
bash
# Set a variable
prefect variable set MY_VAR "my value"

# Get a variable's value
prefect variable get MY_VAR

# Delete a variable
prefect variable delete MY_VAR
Is there anything specific about working with variables that you'd like me to clarify?
d
@Marvin i mean in from my python scripts i have CRUD options with prefect variables? for example:
Copy code
from prefect.variables import set_var
name = "test"
value = "test_value"
set_var(name,value)
m
thought for 52.3 seconds
Yes, you can perform CRUD (Create, Read, Update, Delete) operations with Prefect variables in your Python scripts! However, the syntax is slightly different in Prefect 3.x. Here's the correct way:
Copy code
python
from prefect import variables

# CREATE/UPDATE (Set)
variables.set(name="test", value="test_value")

# READ (Get)
value = variables.get(name="test")
print(value)  # Outputs: test_value

# READ with default value
value = variables.get(name="non_existent", default="fallback_value")

# UPDATE (Set with overwrite)
variables.set(name="test", value="new_value", overwrite=True)

# DELETE (Unset)
variables.unset(name="test")
The syntax you showed (
from prefect.variables import set_var
) was from an older version of Prefect. In Prefect 3.x, we use the
variables
module directly as shown above. You can also work with more complex data types:
Copy code
python
# Dictionary
variables.set("config", {
    "host": "localhost",
    "port": 8080,
    "enabled": True
})

# List
variables.set("allowed_users", ["user1", "user2", "user3"])

# Numbers
variables.set("max_retries", 5)
Remember that variables in Prefect are not encrypted, so avoid storing sensitive information like passwords or API keys. For sensitive data, use Prefect Blocks instead. Would you like me to show you how to work with any specific type of variable or operation?
d
@Marvin AttributeError: module 'prefect.variables' has no attribute 'set'
m
thought for 30.6 seconds
I apologize for my previous incorrect response. You're right, and I need to correct the syntax. In Prefect 3.x, we use the
Variable
class. Here's the correct way to work with variables in your Python scripts:
Copy code
python
from prefect.variables import Variable

# CREATE/UPDATE
Variable.create(name="test", value="test_value")
# or if you want to update an existing variable
Variable.update(name="test", value="new_value")

# READ
value = Variable.get(name="test")
print(value)  # Outputs: test_value

# DELETE
Variable.delete(name="test")
You can also work with different types of values:
Copy code
python
# Dictionary
Variable.create(
    name="config",
    value={"host": "localhost", "port": 8080}
)

# List
Variable.create(
    name="allowed_users",
    value=["user1", "user2", "user3"]
)

# Numbers
Variable.create(
    name="max_retries",
    value=5
)
If you want to check if a variable exists before creating it:
Copy code
python
# Get with default value if it doesn't exist
value = Variable.get(name="my_var", default="default_value")
Remember that variables in Prefect are not encrypted, so avoid storing sensitive information like passwords or API keys. For sensitive data, use Prefect Blocks instead. Would you like me to show you how to work with any specific operation or type of variable?