Visual Studio Code - how to remote debug python co

2019-02-12 18:21发布

问题:

Iam trying to remote dubug python in VSC:

It is main.py file:

print('Hello, World')

debug.py:

import ptvsd
ptvsd.enable_attach('my_secret', address=('0.0.0.0', 7102))
ptvsd.wait_for_attach()

Dockerfile:

FROM python:3.6-slim

EXPOSE 7102

RUN pip install ptvsd

WORKDIR /app

COPY . .

CMD ["python", "debug.py"]

and it is launch.json file:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Attach (Remote Debug)",
            "type": "python",
            "request": "attach",
            "localRoot": "${workspaceRoot}",
            "remoteRoot": "/app",
            "port": 7102,
            "secret": "my_secret",
            "host": "172.17.0.3"
        }
    ]
}

It is a command to build and start container:

docker build -t python-for-debug .

docker run -it -p 7102:7102 python-for-debug

When I run the debugger I get: screen of vs code

No any errors at debug console, not output, not problems. No any output for running container. No docker logs

VS Code version: 1.15.0 Docker version 17.06.0-ce, build 02c1d87

回答1:

Hi you should use ptvsd 3, so change the RUN line in Dockerfile to:

RUN pip3 install ptvsd==3.0.0

also run locally ptvsd installation:

$pip3 install ptvsd==3.0.0

for more info go to https://code.visualstudio.com/docs/python/debugging#_remote-debugging this worked for me, hope it helps.



回答2:

Edit 12-AUG

I setup a test version to see what could be wrong. The issue is that the Visual Studio Code makes not connections to the debugger it failed before connecting only

See the exception is in their JS code.

There is a open issue as well on github

https://github.com/DonJayamanne/pythonVSCode/issues/805

Your best bet is to either add these details to the issue or open a new one

Original Answer:

The behavior you are seeing is actually correct. I saw your screenshot and you had print "Hello World" in your client script and below code is your in remote

import ptvsd
ptvsd.enable_attach('my_secret', address=('0.0.0.0', 7102))
ptvsd.wait_for_attach()

If you see the below url

https://donjayamanne.github.io/pythonVSCodeDocs/docs/debugging_remote-debugging/

Read the below quote

Make the above change in both script files (i.e. scripts on both the local and remote machines) However on the client side, ensure the above two lines are commented out I.e. this is necessary to ensure we have the same line numbers on the server and they match.

The execution of code debugging all happens inside your container. While your code in local machine is more to be able to visualize which the line the code inside the container is



回答3:

I think the problem is that your docker process terminates.

You start your debug.py as a command in the Dockerfile. So Docker starts the task, waits for your debugger to attach and then exits because there is nothing more to do.

Seems you need to place your code into debug.py as the easiest try. And please remember that you cannot place the breakpoint right after

ptvsd.wait_for_attach()

so you'd better write some spare lines between.
Hope this helps.