Vysali R
06/15/2026, 6:26 AMMarvin
06/15/2026, 6:26 AMMarvin
06/15/2026, 6:27 AMpython
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:
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:
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:
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:
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 structureVysali R
06/15/2026, 8:39 AMFerchau Gupta
06/15/2026, 8:47 AMprefect deployment apply my_flow-deployment.yaml ?Marvin
06/15/2026, 8:47 AMMarvin
06/15/2026, 8:48 AMpython
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:
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
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:
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 referenceFerchau Gupta
06/15/2026, 8:48 AMFerchau Gupta
06/15/2026, 8:51 AMload_deployments_from_yaml can I use the prefect.yaml file?Marvin
06/15/2026, 8:51 AMMarvin
06/15/2026, 8:52 AMload_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:
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:
bash
prefect deploy
or:
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.Ferchau Gupta
06/15/2026, 8:55 AMprefect deploy --prefect-file prefect.yaml?Marvin
06/15/2026, 8:55 AMMarvin
06/15/2026, 8:55 AMbash
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:
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`:
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:
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:
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.