Visual Studio代码-如何在Docker中远程调试python代码包含

2024-05-20 20:45:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在VSC中远程dubug python:

它是main.py文件:

print('Hello, World')

调试.py:

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

文档文件:

FROM python:3.6-slim

EXPOSE 7102

RUN pip install ptvsd

WORKDIR /app

COPY . .

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

它是launch.json文件:

{
    "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"
        }
    ]
}

它是生成和启动容器的命令:

docker build -t python-for-debug .

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

当我运行调试器时,我得到: screen of vs code

调试控制台没有任何错误,没有输出,没有问题。没有任何用于运行容器的输出。没有docker日志

VS代码版本:1.15.0 Docker版本17.06.0-ce,版本02c1d87


Tags: 文件dockerpydebug版本appforsecret
3条回答

编辑8月12日

我设置了一个测试版本,看看有什么问题。问题是Visual Studio代码没有与调试程序建立连接,它在只连接之前失败了

看到异常在他们的JS代码中。

Visual Studio Error Log

github上也有一个公开的问题

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

你最好的办法是将这些细节添加到问题中,或者打开一个新的

原始答案:

你看到的行为实际上是正确的。我看到了你的截图,你在客户端脚本中打印了“Hello World”,下面的代码是你的in remote

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

如果你看到下面的网址

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

阅读下面的引文

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.

代码调试的执行都发生在容器中。而您在本地机器中的代码更能够可视化容器中的代码是哪一行

嗨,您应该使用ptvsd 3,所以将Dockerfile中的RUN行更改为:

RUN pip3 install ptvsd==3.0.0

同时在本地运行ptvsd安装:

$pip3 install ptvsd==3.0.0

有关详细信息,请转到https://code.visualstudio.com/docs/python/debugging#_remote-debugging 这对我有用,希望能有帮助。

我想问题是你的docker进程终止了。

在Dockerfile中以命令的形式启动debug.py。因此Docker启动任务,等待调试器附加,然后退出,因为没有更多的事情要做。

似乎您需要将代码放入debug.py中作为最简单的尝试。请记住,不能将断点放在

ptvsd.wait_for_attach()

所以你最好在中间写些空行。
希望这有帮助。

相关问题 更多 >