Ritesh
06/29/2025, 7:49 AMMarvin
06/29/2025, 7:49 AMMarvin
06/29/2025, 7:49 AMpython
from prefect import deploy
from prefect.deployments import DeploymentImage
from prefect.filesystems import GitHub
# Create the deployment
deployment = deploy.Deployment.build_from_flow(
flow_location="{file.py:function_name}", # Your flow's location
name="{deployment_name}",
work_pool_name="{work_pool_name}",
path="{python_path}", # This will set PYTHONPATH
source=GitHub.from_repository(
repository="{repo_link}",
reference="{branch_name}"
)
)
if __name__ == "__main__":
deployment.apply()
Save this in a new file (e.g., create_deployment.py
) and run it with:
bash
python create_deployment.py
A few important notes:
1. Replace all the placeholders in curly braces with your actual values.
2. The flow_location
should point to your flow function using the format "path/to/file.py:function_name"
3. If you need additional environment variables, you can add them like this:
python
deployment = deploy.Deployment.build_from_flow(
# ... other parameters ...
job_configuration={
"env": {
"PYTHONPATH": "{python_path}",
"OTHER_VAR": "value"
}
}
)
4. If your repository is private, you'll need to provide authentication credentials. You can do this by setting up a GitHub token in your Prefect environment.
This approach gives you the same functionality as your YAML file but allows you to create the deployment programmatically. It also gives you more flexibility to add logic or conditions if needed.
Let me know if you need any clarification on specific parts of this setup!Ritesh
06/29/2025, 8:05 AMMarvin
06/29/2025, 8:05 AMMarvin
06/29/2025, 8:05 AMpython
from prefect import flow
from prefect.deployments import Deployment
from prefect.filesystems import GitHub
# Create a deployment
deployment = Deployment(
name="{deployment_name}",
flow_name="{function_name}", # The name of your flow function
work_pool_name="{work_pool_name}",
path="{file.py}", # Path to your flow file relative to repo root
job_variables={
"env": {
"PYTHONPATH": "{python_path}"
}
}
).from_source(
source="{repo_link}",
branch="{branch_name}"
)
if __name__ == "__main__":
deployment.apply()
Save this in a separate file (e.g., create_deployment.py
) and run it:
bash
python create_deployment.py
Key differences from your YAML:
1. The entrypoint
is split into path
(the file) and flow_name
(the function name)
2. Git clone is handled automatically by .from_source()
3. Environment variables are set through job_variables
For example, if your actual values were:
python
deployment = Deployment(
name="my-deployment",
flow_name="my_flow",
work_pool_name="my-pool",
path="flows/main.py",
job_variables={
"env": {
"PYTHONPATH": "/path/to/project"
}
}
).from_source(
source="<https://github.com/username/repo>",
branch="main"
)
Additional options you might find useful:
1. If you need more environment variables:
python
job_variables={
"env": {
"PYTHONPATH": "{python_path}",
"OTHER_VAR": "value",
"ANOTHER_VAR": "value2"
}
}
2. If you want to add tags or schedule:
python
deployment = Deployment(
# ... other parameters ...
tags=["tag1", "tag2"],
interval="0 0 * * *" # Cron schedule
)
3. If your repository is private, you'll need to configure Git authentication through Prefect's settings.
This approach gives you the same functionality as your YAML file but with the flexibility of Python. You can add conditions, loops, or other logic as needed.Ritesh
06/29/2025, 8:09 AMMarvin
06/29/2025, 8:09 AMMarvin
06/29/2025, 8:09 AMdeploy
function instead:
python
from prefect import deploy
if __name__ == "__main__":
deployment = deploy(
name="{deployment_name}",
flow_path="{file.py}", # Path to your flow file relative to repo root
flow_name="{function_name}", # The name of your flow function
work_pool_name="{work_pool_name}",
job_variables={
"env": {
"PYTHONPATH": "{python_path}"
}
}
).from_source(
source="{repo_link}",
branch="{branch_name}"
)
For example, with actual values:
python
from prefect import deploy
if __name__ == "__main__":
deployment = deploy(
name="my-deployment",
flow_path="flows/main.py",
flow_name="my_flow",
work_pool_name="my-pool",
job_variables={
"env": {
"PYTHONPATH": "/path/to/project"
}
}
).from_source(
source="<https://github.com/username/repo>",
branch="main"
)
Additional options you might want to use:
1. Adding tags and schedule:
python
deployment = deploy(
# ... other parameters ...
tags=["tag1", "tag2"],
interval="0 0 * * *" # Cron schedule
)
2. Adding more environment variables:
python
job_variables={
"env": {
"PYTHONPATH": "/path/to/project",
"OTHER_VAR": "value",
"ANOTHER_VAR": "value2"
}
}
Save this in a separate Python file (e.g., create_deployment.py
) and run it:
bash
python create_deployment.py
This is the current, recommended way to create deployments programmatically in Prefect 3.4.4+. The deploy
function provides a cleaner, more straightforward API compared to the previous approaches.