Debugging Prefect Flows with breakpoints. Hello f...
# prefect-community
m
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:
Copy code
$ 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)
discourse 1
@Kevin Mullins... your useful just answer disappeared?
k
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.
upvote 1
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:
Copy code
import prefect
from prefect import task, Flow


@task
def say_hello():
    <http://prefect.context.logger.info|prefect.context.logger.info>("hello")

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

if __name__ == "__main__":
    flow.run()
m
Oh yes, I was using a small pytest file with flow.run([params]) but the main guard is also nice.
k
Here is an example launch config if you wanted to go that route (for instance if you didn’t want the main guard)
Copy code
{
  "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
}
👍 2
m
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?
k
Both of them should show them, vs code should create a python debug terminal
🙌 1
m
@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",
1023 Views