Python Remote Debug Server not stopping at breakpoint when using Okteto with PyCharm

1.2k Views Asked by At

Overview

I am currently following How to Develop and Debug Python Applications on Kubernetes in order to debug a simple hello-world deployment using Okteto.

Everything seems to work but for some reason the debugger is not stopping at the breakpoint in PyCharm. The breakpoint is either ignored or never set. All I get is a warning after connecting to the debug server:

pydev debugger: warning: trying to add breakpoint to file that does not exist: /Users/sfalk/workspaces/okteto-example/src/service/router.py (will have no effect)

The biggest difference might be that I am using FastAPI instead of Flask.

A Simple Hello-World Service

I've a slightly different setup, but essentially I am trying to debug this router.py, which is pretty much the same thing as the app.py (see repository) from that tutorial I linked:

import os
import socket

import uvicorn
from fastapi import FastAPI
from pydantic import BaseModel

REMOTE_DEBUGGING = bool(os.environ.get("REMOTE_DEBUGGING", False))

app = FastAPI()


class Message(BaseModel):
    message: str


@app.get("/")
def hello_world():
    return Message(message=f"Hello {socket.gethostname()}")


if __name__ == "__main__":
    print("Starting server...")
    print(f"REMOTE_DEBUGGING: {REMOTE_DEBUGGING}")

    if REMOTE_DEBUGGING:
        import pydevd_pycharm

        print("Connecting to debugger...")
        print(f"pydevd-pycharm version: {pydevd_pycharm.VERSION_STRING}")
        pydevd_pycharm.settrace("0.0.0.0", port=9000, stdoutToServer=True, stderrToServer=True)

    uvicorn.run(
        "service.router:app",
        port=8080,
        access_log=True,
        log_level="debug",
        reload=True,
        debug=True,
    )

Okteto & Python Remote Debug Server

This here is my okteto.yaml:

name: depl-router
command: bash
sync:
- .:/usr/src/app
forward:
- 8080:8080
reverse:
- 9000:9000

I have set up a run-configuration for the Python Remote Debug Server (PRDS) which has the following settings:

IDE host name: localhost
Port:          9000

After creating the Docker-image and having it deployed on the local Kubernetes cluster, I am using Docker Desktop on my local machine, I call okteto up and, for now, run the FastAPI server by hand as you can see in the log-output below. This is where we get the warning from the pydev debugger.

$ okteto up
 i  Using default @ docker-desktop as context
 ✓  Files synchronized
    Context:   docker-desktop
    Namespace: default
    Name:      depl-router
    Forward:   8080 -> 8080
    Reverse:   9000 <- 9000

root@depl-router-okteto-f49f5fc6-6gjxc:/usr/src/app# REMOTE_DEBUGGING=1 PYTHONPATH=src/. python src/service/router.py
Starting server...
REMOTE_DEBUGGING: True
Connecting to debugger...
pydevd-pycharm version: 213.6777.50
pydev debugger: warning: trying to add breakpoint to file that does not exist: /Users/sfalk/workspaces/okteto-example/src/service/router.py (will have no effect)
INFO:     Uvicorn running on http://127.0.0.1:8080 (Press CTRL+C to quit)
INFO:     Started reloader process [232] using statreload
INFO:     Started server process [240]
INFO:     Waiting for application startup.
INFO:     Application startup complete.

After connecting, this is what I am seeing in the PRDS:

Starting debug server at port 9,000
Use the following code to connect to the debugger:
import pydevd_pycharm
pydevd_pycharm.settrace('localhost', port=9000, stdoutToServer=True, stderrToServer=True, suspend=False)
Waiting for process connection…
Connected to pydev debugger (build 213.6777.50)
[32mINFO[0m:     Uvicorn running on [1mhttp://127.0.0.1:8080[0m (Press CTRL+C to quit)
[32mINFO[0m:     Started reloader process [[36m[1m152[0m] using [36m[1mstatreload[0m

Sending HTTP-Requests & Reloading is Working

The good news is, that I am able to send a GET request and the deployed service is responding:

$ curl localhost:8080
{"message":"Hello depl-router-okteto-f49f5fc6-6gjxc"}

I am also able to modify the code locally and I can see that Okteto is correctly swapping modified files in the deployment/pod.

Breakpoints Not Working

The main problem here is that setting a breakpoint does not work. This means that after setting a breakpoint and sending a GET request, PyCharm IDE and the PRDS do not respond at all. I'd expect a warning if the breakpoint could not have been set, but since there is no warning I assume that the breakpoint was either set, or there was never an attempt to do so.

After connecting to the PRDS, PyCharm telling my that the remote router.py file could not be mapped to it's local twin:

Remote file /usr/src/app/src/service/router.py is mapped to the local path ./src/service/router.py and can't be found. You can continue debugging, but without the source.

For this, I've tried to edit the RPDS configuration and set the mapping accordingly:

enter image description here

This is the directory which I am copying the repository to in my Docker image but we can also confirm this by looking at the run command for the FastAPI server which gets started from /usr/src/app (see the output after calling okteto up).

However, setting the path mapping has no effect. I can use the auto-detect option, which correctly identifies the local file, but it does not resolve the problem.

1

There are 1 best solutions below

0
On

Stefan, would it be possible for you to post a sample repo that reproduced your issue?

I retested our python getting started guide today with the updated 2.0.2 cli, and breakpoints are getting hit in today's version of PyCharm Professional.

working branch https://github.com/okteto/python-getting-started/tree/v2manifest