Debugging Prefect flows with breakpoints in Visual Studio Code (VSCode)

View in #prefect-community on Slack

Martin_T @Martin_T: Debugging Prefect Flows with breakpoints.

Hello friends. I love coding Python in VSCode, the debugging with breakpoints make me so efficient.
But the debugging fun stops with Prefect Flows, when we initialize them from CLI:

$ prefect run --path myflow.py --log-level INFO --param myparam=val

Here the development flow becomes rather blackboxed/trial-and-error.
Is it possible to somehow set breakpoints in VSCode, start a prefect flow from CLI, and attach the debugger to the python process?

(Running with the LocalExecutor)
@Kevin_Mullins… your useful just answer disappeared?

Kevin_Mullins @Kevin_Mullins: Yes this is possible. You should just be able to add a new python launch configuration with prefect run command and arguments. Another option is to add a call to flow.run in your flow and using the debug current file feature.
Sorry, I realized I gave the wrong info first, was clarifying
For the example of calling flow.run, some people use a main guard for debugging/running, here is an example that would work with debug current file:

import prefect
from prefect import task, Flow


@task
def say_hello():
    logger.info("hello")

with Flow("test-debug") as flow:
    say_hello()

if __name__ == "__main__":
    flow.run()

Martin_T @Martin_T: Oh yes, I was using a small pytest file with flow.run([params]) but the main guard is also nice.

Kevin_Mullins @Kevin_Mullins: Here is an example launch config if you wanted to go that route (for instance if you didn’t want the main guard)

        {
            "name": "Python: Flow Debug Example",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/.venv/bin/prefect",
            "args": [
                "run",
                "--path",
                "dev_flows/debug_flow.py"
            ],
            "console": "integratedTerminal",
            "justMyCode": true
        }

Martin_T @Martin_T: I think the launch config has the upside to be able to show info/debug logs to the terminal. Or is it also possible with the flow.run() debug method?

Kevin_Mullins @Kevin_Mullins: Both of them should show them, vs code should create a python debug terminal

Martin_T @Martin_T: @Kevin_Mullins thanks a lot, both works great.

Launch config is a bit clunkly because prefect is installed with pipenv:
program": "/home/[USER]/.local/share/virtualenvs/[SOURCEFOLDER]-[HASH]/bin/prefect",