How to runremote debugging for fastapi based python web app in vscode

Hi,
I am running an webapplication in python in vs code Though okteto development env is up, I am unable to run in debug mode.
VS Code allows attach option. Now when run debugger in vscode via launch.json its unable to find remote service. Please help. Below launch.json

 {
            "name": "Python Debugger: Remote Attach1",
            "type": "debugpy",
            "request": "attach",
            "connect": {
                "host": "localhost",
                "port": 5678
            },
            "pathMappings": [
                {
                    "localRoot": "${workspaceFolder}",
                    "remoteRoot": "."
                }
            ]
        }

Hi @midprasanta,

Could you please provide your Okteto manifest? Please make sure to redact any sensitive values from it.

Please attach any screenshots or log outputs you have when the error occurs.

If needed, you can refer to our guide for Python development in Okteto. Step 4 specifically discusses remote debugging. You can find the guide here:

Hi @provecho ,
Below manifest file. It is working now when I comment reverse port mapping


deploy:
  - kubectl apply -f k8s.yml
dev:
  hello-world:
    command: bash
    environment:
      - FLASK_ENV=development
    sync:
      - .:/usr/src/app
    forward:
      - 8080:8080
      - 5678:5678
    reverse:
      # - 9000:5678
    volumes:
      - /root/.cache/pip

However code does not reload on its own when I make change in source. Any suggestion please.

Hi @midprasanta,

For debugging in VS Code with Okteto, ensure file synchronization is working properly. Check file sync status with okteto status --info. Confirm files aren’t ignored in .stignore. Details:

Regarding code hot reload, consider using a custom command in okteto up. For Flask, try flask run --debug. Documentation:

https://flask.palletsprojects.com/en/latest/quickstart/#debug-mode

HI @provecho,
Is there anyway I can reduce time of make application up? I have virtual env copied in okteto pod. But without installing its not working.

deploy:
  - helm uninstall abcd
  - helm install abcd ./charts/abcd--set image.repository="k3d-abcd.localhost:5000/abcd" --set env.OPENSEARCH_HOST=172.17.0.1 --set env.OPENSEARCH_INDEX=abcd-articles --kube-context k3d-abcd--namespace abcd
dev:
  abcd:
    image: okteto/python:3
    command: pip install poetry==1.7.0 && pip install uvicorn && chmod 777 ./.venv/bin/activate && ./.venv/bin/activate && poetry install && poetry shell && bash
    forward:
      - 8080:8080
      - 5678:5678
    reverse:
    # - 9000:9000
    volumes:
    - /root/.cache/pypoetry
    sync:
    - .:/usr/src/app  

Hi @midprasanta,

You can improve your setup by adding a build section in the Okteto manifest to create a specific container image for the development environment. Check out the example in the okteto/movies repository, specifically for the service frontend. Utilize the target property in the Dockerfile to build up to a certain stage and set the image in the dev section using OKTETO environment variables. Here are the relevant links for reference:

Example Okteto manifest: okteto/movies

Okteto manifest documentation: Build Object

Okteto environment variables: Built-in Environment Variables for Images in Okteto Registry

Hi @provecho,

Somehow by launch.json is changed. Now when I reconfigure it it is not working.
Below my launch.json

{
    "version": "0.2.0",
    "configurations": [
        
        {
            "name": "Python Debugger: Remote Attach",
            "type": "debugpy",
            "request": "attach",
            "connect": {
                "host": "localhost",
                "port": 5678
            },
            "pathMappings": [
                {
                    "localRoot": "${workspaceFolder}",
                    "remoteRoot": "."
                }
            ]
        }
    ]
}

okteto.yaml

name: python-getting-started
build:
  hello-world:
    context: .
    dockerfile: Dockerfile
    image: midprasanta/python-hello-world:1.0.0
deploy:
  - kubectl apply -f k8s.yml
dev:
  hello-world:
    command: bash
    environment:
      - FLASK_ENV=development
    sync:
      - .:/usr/src/app
    forward:
      - 8080:8080
      - 5678:5678
    reverse:
      #- 5678:5678
    volumes:
      - /root/.cache/pip

Not sure how it broke. When I run debug in vscode somehow its unable to connect and debug does not start.

Hi @midprasanta,

To set up remote debugging for your Python application using Okteto and VSCode, please check the official VSCode docs.

Are you running the debugpy server inside the Okteto pod? This is necessary for remote debugging to work.

Hi @provecho
Now I am able to run in remote debug with below code in app.py

  debugpy.listen(('0.0.0.0', 5678))
  debugpy.wait_for_client()
  
  app.run(host='0.0.0.0', port=8000)

launch.json

{
    "version": "0.2.0",
    "configurations": [
        
        {
            "name": "Python Debugger: Remote Attach",
            "type": "debugpy",
            "request": "attach",
            "connect": {
                "host": "localhost",
                "port": 5678
            },
            "pathMappings": [
                {
                    "localRoot": "${workspaceFolder}",
                    "remoteRoot": "/usr/src/app"
                }
            ]
        }
    ]
}

Running python -Xfrozen_modules=off app.py inside development container.

However auto reload of change not working. O/P of sync status info

okteto status --info
 i  Using user1@ k3d-cluster as context
 i  Local syncthing url: http://localhost:35815
 i  Remote syncthing url: http://localhost:39039
 i  Syncthing username: okteto
 i  Syncthing password: e5996867-8d23-4120-a79f-d0532aabd1f5

Hi @midprasanta,

Hot reloading depends on the process you are running in your development setup. For Python, you can use tools like watchdog or reload to achieve this.

To troubleshoot issues with synchronization, try okteto status --info and observe the last line where the percentage is shown. Alternatively, you can manually test synchronization by changing a file locally and checking it remotely using bash.

Hi @provencho,

Finally I could run remote debug and hot deploy with fastAPI and uvicorn library.
Below code to run both debugpy debug server as well as uvicorn server. Code from main.py

if __name__ == "__main__":
    print('Starting app...')
    if(os.getenv('CURRENT_ENV', 'prod') == 'dev'):
        print('Starting app in debugmode')
        debugpy.listen(('0.0.0.0', 5678))
        debugpy.wait_for_client()
    uvicorn.run(
        "main:app",
        host="0.0.0.0",
        port=8081,
        reload=True
    )