[go: nahoru, domu]

Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to setup a remote debugger with vscode #707

Closed
Msordet opened this issue Nov 13, 2019 · 11 comments
Closed

How to setup a remote debugger with vscode #707

Msordet opened this issue Nov 13, 2019 · 11 comments
Labels
question Question or problem question-migrate

Comments

@Msordet
Copy link
Msordet commented Nov 13, 2019

Description

Hi, I am trying to set up the vscode debugger with a remote container for FastAPI.
Here's my launch.json:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Attacher",
            "type": "python",
            "request": "attach",
            "port": 5678,
            "host": "localhost",
            "pathMappings": [
                {
                    "localRoot": "${workspaceFolder}",
                    "remoteRoot": "/app"
                }
            ]
        }
    ]
}

In my docker-compose file, I export the ports 5678 (debugger) and 8000 (api) and the command is: uvicorn myapp.main:app --host 0.0.0.0 --port 8000 --reload
.

In main.py, I've added the followling lines before uvicorn.run(...):

import ptvsd

ptvsd.enable_attach(address=('0.0.0.0', 5678), redirect_output=True)

When I try to run the debugger, I have this error happening in the vscode console (Help > Toggle Developer Tools):

  ERR timeout after 500 ms: Error: timeout after 500 ms
    at t.RawDebugSession.handleErrorResponse (file:///Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.desktop.main.js:2798:1003)
    at file:///Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.desktop.main.js:2798:434
    at async t.RawDebugSession.shutdown (file:///Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.desktop.main.js:2796:536)
    at async file:///Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.desktop.main.js:5182:409

In my process I checked a lot of resources to no avail such as:

@Msordet Msordet added the question Question or problem label Nov 13, 2019
@khambadkone
Copy link

I had a similar issue on my Mac, and it was due to mono not being installed (https://www.mono-project.com/download/stable/) and also not set-up on my path. via export PATH=/Library/Frameworks/Mono.framework/Versions/Current/bin/:${PATH}
This was causing the debugger to throw the same 500 errors.

@NumanIjaz
Copy link

@Msordet did you find any solution to this problem?

@mcauto
Copy link
mcauto commented Apr 24, 2020

same here..
Did you solve it?

@mcauto
Copy link
mcauto commented May 4, 2020

@paxcodes
Copy link
Contributor

I was able to debug by following the docs in VS Code but instead of adding debugpy.listen(5678) as they have suggested, I put in debugpy.listen(("0.0.0.0", 5678))

My docker-compose setup is based on the full-stack-fastapi-postgresql cookiecutter provided by tiangolo.

@Kludex
Copy link
Sponsor Collaborator
Kludex commented Jan 9, 2021

There's no need for Python code: https://github.com/Kludex/fastapi-docker-debug

@TheCDC
Copy link
TheCDC commented Jan 15, 2021

@Kludex I used your solution as a template and I can debug and hit breakpoints, thank you.

Launch.json

{
  "configurations": [
    {
      "name": "Python: Remote Attach",
      "type": "python",
      "request": "attach",
      "port": 5678,
      "host": "localhost",
      "pathMappings": [
        {
          "localRoot": "${workspaceFolder}/app",
          "remoteRoot": "/app"
        }
      ]
    }
  ]
}

docker-compose.debug.yml

version: "3.3"
services:
  backend:
    environment:
      - SERVER_NAME=${DOMAIN?Variable not set}
      - SERVER_HOST=https://${DOMAIN?Variable not set}
      # Allow explicit env var override for tests
      - SMTP_HOST=${SMTP_HOST}    
    env_file:
      - .env
    build:
      context: ./backend
      dockerfile: backend.dockerfile
      args:
        INSTALL_DEV: ${INSTALL_DEV-false}
    command:
      [
        "sh",
        "-c",
        "pip install debugpy && python -m debugpy --wait-for-client --listen 0.0.0.0:5678 -m uvicorn app.main:app --reload --host 0.0.0.0 --port 8000",
      ]
    volumes:
      - ./backend/app:/app
    ports:
      - 8000:8000
      - 5678:5678


networks:
  traefik-public:
    # For local dev, don't expect an external Traefik network
    external: false

Command to run backend in debug mode

docker-compose -f docker-compose.debug.yml up

@Kludex
Copy link
Sponsor Collaborator
Kludex commented Jan 15, 2021

You're welcome. 😗

@ccrvlh
Copy link
ccrvlh commented Dec 8, 2021

@tiangolo @Kludex suggestion to close this issue (or move it to a discussion). I'm reading this more like a VSCode/debugging setup issue, rather than something directly related to FastAPI code base.

@tiangolo
Copy link
Owner

Thanks for the discussion everyone!

And nice job @Kludex ! 🚀

@github-actions
Copy link
Contributor

Assuming the original need was handled, this will be automatically closed now. But feel free to add more comments or create new issues or PRs.

@tiangolo tiangolo changed the title [QUESTION] How to setup a remote debugger with vscode How to setup a remote debugger with vscode Feb 24, 2023
@tiangolo tiangolo reopened this Feb 28, 2023
@github-actions github-actions bot removed the answered label Feb 28, 2023
Repository owner locked and limited conversation to collaborators Feb 28, 2023
@tiangolo tiangolo converted this issue into discussion #8004 Feb 28, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
question Question or problem question-migrate
Projects
None yet
Development

No branches or pull requests

9 participants