<@ULVA73B9P> how do i debug the prefect and python...
# marvin-in-the-wild
r
@Marvin how do i debug the prefect and python code locally?
m
To debug Prefect and Python code locally, you can follow these general steps: 1. Set Up Local Prefect Server: Start with configuring a local Prefect server. Run the following command to set the API URL to point to a local Prefect server instance:
Copy code
shell
   prefect config set PREFECT_API_URL="<http://127.0.0.1:4200/api>"
This is necessary when running Prefect inside a container. (Prefect Docs) 2. Local SQLite Database and Dashboard: Use the local SQLite database (
~/.prefect/prefect.db
) to inspect flow runs after you start the Prefect server using
prefect server start
. You can also use the local UI dashboard to monitor your flows. (Blog Post) 3. Code Debugging: As for debugging the code, you can write your tasks and flows as normal Python functions and then execute them. At this stage, you can include print statements or use Python's built-in debugger (
pdb
) to step through your code. Here is a basic example of a Prefect flow with tasks that can be run and debugged locally:
Copy code
python
from prefect import flow, task

@task(task_run_name="Adding {x} and {y}")
def add(x: int, y: int) -> int:
    """just a python function with a fancy hat on!"""
    return x + y

@flow
def my_flow():
    """tasks must be called in the flow to be executed (at this time)"""
    first_result = add(1, 2) 
    second_result = add(first_result, first_result)
    return add(first_result, second_result)

if __name__ == "__main__":
    # run the flow
    my_flow()
    
    # you can run tasks' functions directly outside of a flow if you want
    # but it won't be tracked by Prefect, it's just a normal function
    assert add.fn(1, 2) == 3
In the flow
my_flow
, the
add
task is executed. You can debug this task by running it directly using the syntax
add.fn(arg1, arg2)
which won't be tracked by Prefect, allowing you to test and debug the function logic directly. (Example Code) Remember to tailor your debugging procedures to your specific development environment's details and preferences.
r
i also have prefects blocks in the code but the local environment has no connection to the prefect server, how do i debug given these infos?
n
the easiest way is actually to just run the file
python my_flow.py
you don't need a local server, by default we'll use an ephemeral API