简体   繁体   English

在VS Code中使用Python和节点进程调试应用程序

[英]Debugging an Application with Python & Node Processes In VS Code

I recently have picked up VS Code because I have a new component written in NodeJS that I need to be able to debug, and learned that debugging support was built in to VS Code. 我最近选择了VS Code,因为我有一个需要用NodeJS编写的新组件才能调试,并且了解到VS Code内置了调试支持。 I have debugging possible in PyCharm for Python, but JS support is not available in PyCharm's community edition so I wanted to move my environment to VSC. 我可以在PyCharm for Python中进行调试,但是PyCharm的社区版中没有JS支持,因此我想将环境迁移到VSC。

The application I am working with runs mainly on Python, and there is a new component that we've added that runs on NodeJS on a separate port. 我正在使用的应用程序主要在Python上运行,并且我们添加了一个新组件,该组件在NodeJS的单独端口上运行。 I have got my configuration set up to launch the main application, which spins up the Python and NodeJS servers. 我已经设置好配置以启动主应用程序,该应用程序启动了Python和NodeJS服务器。

Now, I would like to set it up to pause at my breakpoints when I set them in the NodeJS and Python code. 现在,当我在NodeJS和Python代码中设置断点时,我想将其设置为在断点处暂停。 I thought this could be done with the configuration with "request": "attach" , but when I match the port to my application's ports I only get an error ( Failed to attach (connect ECONNREFUSED 127.0.0.1:8888) ). 我以为可以用"request": "attach"的配置来完成,但是当我将端口与应用程序的端口匹配时,我只会得到一个错误Failed to attach (connect ECONNREFUSED 127.0.0.1:8888) )。 I know this is probably because the port is not running my app yet at this point, but when I go back to run the attach configuration by itself, I get a notification about how the process has terminated unexpectedly. 我知道这可能是因为端口此时尚未运行我的应用程序,但是当我自己单独运行附加配置时,我会收到有关进程如何意外终止的通知。

So here I am, I don't know what I need to do to get my breakpoints to stop so I can step through my Python and NodeJS code. 所以我在这里,我不知道我需要做些什么来使我的断点停止,这样我才能逐步完成Python和NodeJS代码。

Here is my launch.json file. 这是我的launch.json文件。 The Python process runs on port 8888 and the NodeJS process runs on port 8895. Python进程在端口8888上运行,而NodeJS进程在端口8895上运行。

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "CloudDrafts",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/obrien.server/project/apps/obrien/runner.py",
            "args": ["-d", "${workspaceFolder}/obrien.server/_data"],
            "console": "externalTerminal",
            "pythonPath": "${workspaceFolder}/venv/bin/python2.7",
            "cwd": "${workspaceFolder}/obrien.server/project/apps/obrien",
            "env": {
                "PYTHONPATH": "${workspaceFolder}/obrien.server/project"
            }
        },
        {
            "name": "Attach (Python)",
            "type": "python",
            "request": "attach",
            "port": 8888,
            "host": "localhost"
        },
        {
            "name": "Attach (Node.js)",
            "type": "node",
            "request": "attach",
            "port": 8895,
            "address": "localhost"
        }
    ],
    "compounds": [
        {
            "name": "Debug CloudDrafts",
            "configurations": ["CloudDrafts", "Attach (Python)", "Attach (Node.js)"]
        }
    ]
}

To clarify, I have installed the Python extension, and have been through as much of Microsoft's documentation as I could find, but now I'm not really getting it and not having much luck finding what I need. 为了澄清起见,我已经安装了Python扩展,并且已经浏览了尽可能多的Microsoft文档,但是现在我并没有真正理解它,也没有运气找到我需要的东西。

What must be done to set up VS Code to allow for breakpoint debugging for an application running Python and NodeJS processes in separate ports? 要设置VS Code以允许在单独的端口中运行Python和NodeJS进程的应用程序进行断点调试,必须做什么?

The Python extension's debugger is only set up to debug Python code on both ends, so the error you are seeing in the attach scenario is because it's trying to find another instance of the ptvsd debugger on the other end and only finding Node. Python扩展程序的调试器仅设置为在两端调试Python代码,因此在attach方案中看到的错误是因为它试图在另一端查找ptvsd调试器的另一个实例,并且仅找到Node。 If you change to something like debugging by module or file then Python debugging will work again. 如果您更改为通过模块或文件进行调试,则Python调试将再次起作用。

As for then attaching to a live Node process to debug the other side, I'm not sure how to do that. 至于然后附加到活动的Node进程以调试另一端,我不确定该怎么做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM