<@ULVA73B9P> can you give me an example template o...
# ask-marvin
v
@Marvin can you give me an example template of adding a cron schedule to my prefect flow? I am using prefect 2.2025
m
thought for 107.2 seconds
Yep — since you’re on Prefect 2.x, here’s a simple template for adding a cron schedule to a flow deployment. Python example
Copy code
python
from prefect import flow
from prefect.deployments import Deployment
from prefect.client.schemas.schedules import CronSchedule

@flow
def my_flow():
    print("Hello from Prefect!")

deployment = Deployment.build_from_flow(
    flow=my_flow,
    name="my-cron-deployment",
    work_queue_name="default",
    schedule=CronSchedule(
        cron="0 9 * * *",          # every day at 9:00
        timezone="America/New_York"
    ),
)

if __name__ == "__main__":
    deployment.apply()
What this does: - creates a deployment for
my_flow
- adds a cron schedule - runs every day at
9:00 AM
- uses the
America/New_York
timezone Common cron examples -
0 9 * * *
→ every day at 9am -
*/15 * * * *
→ every 15 minutes -
0 0 * * MON
→ every Monday at midnight -
0 6 1 * *
→ 6am on the 1st of every month YAML-style deployment example If you’re using a deployment YAML:
Copy code
yaml
name: my-cron-deployment
flow_name: my_flow
work_queue_name: default
schedule:
  cron: "0 9 * * *"
  timezone: "America/New_York"
Then apply it with:
Copy code
bash
prefect deployment apply deployment.yaml
CLI option You can also create/build a deployment with a cron schedule from the CLI in Prefect 2.x:
Copy code
bash
prefect deployment build path/to/file.py:my_flow \
  --name my-cron-deployment \
  --work-queue default \
  --cron "0 9 * * *"
Then apply the generated YAML:
Copy code
bash
prefect deployment apply my_flow-deployment.yaml
Important note Because you're on Prefect 2.x, using
Deployment.build_from_flow()
is correct here. In Prefect 3.x this changed, but for your version this is the right pattern. Helpful docs: - Prefect 2.x deployments - Prefect 2.x schedules - Schedule schema reference (API reference; behavior differs by version, so use 2.x docs above first) If you want, I can also give you: - a full working example with
serve()
/ agent setup
- a multiple cron schedules example - or a
prefect.yaml
/ deployment file
tailored to your project structure
v
I want to add an individual schedule in prefect.yaml file under deployement
f
@Marvin is there any python API analogous to
prefect deployment apply my_flow-deployment.yaml
?
m
thought for 60.3 seconds
Yes — in Prefect 2.x, the closest Python equivalent is:
Copy code
python
from prefect.deployments import Deployment

deployment = Deployment.load_from_yaml("my_flow-deployment.yaml")
deployment_id = deployment.apply()
If you're in an async context, you can also do:
Copy code
python
from prefect.deployments import Deployment

deployment = await Deployment.load_from_yaml("my_flow-deployment.yaml")
deployment_id = await deployment.apply()
So conceptually: -
prefect deployment apply my_flow-deployment.yaml
- is roughly equivalent to -
Deployment.load_from_yaml(...).apply()
Example
Copy code
python
from prefect.deployments import Deployment

deployment = Deployment.load_from_yaml("my_flow-deployment.yaml")
deployment_id = deployment.apply()

print(f"Deployment registered: {deployment_id}")
A couple notes: -
load_from_yaml()
reads the deployment spec from YAML -
apply()
registers that deployment with the Prefect API - if your YAML references blocks/storage/infrastructure, Prefect may need access to your API/server to hydrate those references properly There is also a helper for multiple deployments from one YAML file:
Copy code
python
from prefect.deployments import load_deployments_from_yaml

registry = load_deployments_from_yaml("deployments.yaml")

for deployment in registry.deployments:
    deployment.apply()
If you want, I can give you: - a full script that mirrors CLI apply behavior - or a comparison of
Deployment.build_from_flow(...).apply()
vs
load_from_yaml(...).apply()
. Relevant docs: - Prefect 2.x deployments - `prefect.deployments` API reference
f
@Vysali R you would have to tag the marvin ai bot everytime you want to get a response from it
@Marvin with
load_deployments_from_yaml
can I use the prefect.yaml file?
m
thought for 76.3 seconds
No —
load_deployments_from_yaml()
is not for
prefect.yaml
. It expects the older deployment YAML format that maps directly to deprecated
Deployment
objects, not the newer project-style
prefect.yaml
structure. So: -
load_deployments_from_yaml("some-file.yaml")
old deployment spec format -
prefect.yaml
new project/deploy config format - these are not interchangeable If you point
load_deployments_from_yaml()
at a
prefect.yaml
, it won’t behave as a proper loader for that format. What to do instead with
prefect.yaml
If you want to work with
prefect.yaml
in Python, the practical option is to parse it yourself:
Copy code
python
import yaml

with open("prefect.yaml", "r") as f:
    config = yaml.safe_load(f)

deployments = config.get("deployments", [])
for d in deployments:
    print(d["name"])
    print(d["entrypoint"])
If your goal is to apply/deploy from
prefect.yaml
, the intended interface is the CLI:
Copy code
bash
prefect deploy
or:
Copy code
bash
prefect deploy --prefect-file prefect.yaml
So the short answer is: Can
load_deployments_from_yaml
use
prefect.yaml
?
No. Best path instead: - use
prefect deploy
for
prefect.yaml
- or parse
prefect.yaml
yourself in Python if you need to inspect/manipulate it programmatically Docs: - `prefect deploy` guide - Projects and `prefect.yaml` - `prefect.deployments` API reference If you want, I can also show you a Python script that reads
prefect.yaml
and programmatically deploys each entry
in a way that approximates
prefect deploy
.
f
@Marvin is there any python api analogous to
prefect deploy --prefect-file prefect.yaml?
m
thought for 7.0 seconds
Not really — there is not a stable, public Python API that is the direct equivalent of:
Copy code
bash
prefect deploy --prefect-file prefect.yaml
In Prefect 2.x,
prefect.yaml
is primarily designed to be consumed by the CLI, not by a public high-level Python function like:
Copy code
python
deploy_from_prefect_file("prefect.yaml")
So the short answer is: No, there isn’t a supported one-liner Python API analogous to
prefect deploy --prefect-file prefect.yaml
.
What you can do instead 1. Use the CLI This is the intended interface for `prefect.yaml`:
Copy code
bash
   prefect deploy --prefect-file prefect.yaml
2. Parse
prefect.yaml
yourself in Python
If you need Python control, you can load the file with
yaml.safe_load()
and then use the deployment data yourself. Example:
Copy code
python
   import yaml

   with open("prefect.yaml", "r") as f:
       config = yaml.safe_load(f)

   for deployment in config.get("deployments", []):
       print(deployment["name"])
       print(deployment["entrypoint"])
       print(deployment.get("schedule"))
3. Create deployments programmatically instead of using
prefect.yaml
If your goal is “deploy from Python,” it’s usually cleaner to define deployments in Python directly rather than trying to reproduce the CLI’s
prefect.yaml
processing. --- Why this distinction exists
prefect deploy
does more than just “read YAML”: - reads
prefect.yaml
- resolves defaults - processes build/push/pull steps - handles deployment configuration merging - registers deployments with the API That orchestration is exposed as a CLI workflow, not as a simple public Python API. If you need Python automation You have two practical choices: - shell out to the CLI from Python:
Copy code
python
import subprocess

subprocess.run(
    ["prefect", "deploy", "--prefect-file", "prefect.yaml"],
    check=True,
)
- or manage deployment creation natively in Python instead of relying on
prefect.yaml
--- Bottom line -
prefect deployment apply my.yaml
→ has a Python analogue via
Deployment.load_from_yaml(...).apply()
-
prefect deploy --prefect-file prefect.yaml
no equivalent public Python API Docs: - `prefect deploy` guide - Projects and `prefect.yaml` If you want, I can show you either: - a Python wrapper around
prefect deploy
, or - a fully programmatic Python deployment pattern that avoids
prefect.yaml
entirely.