简体   繁体   中英

Connect Visual Studio 2019 debugger to Python process in VS Code

I have a Python project setup in VS Code. The Python project calls a C++ library, which I'd like to debug.

Normally...

Normally, I'd debug as follows:

  1. Set a breakpoint in a test within the Python project in VS Code (version 1.70.3). Note: I am using pytest .
  2. Debug the Python test via Test Explorer in VS Code. Wait until execution pauses on the breakpoint set above.
  3. In Visual Studio Professional 2019, set a breakpoint in the C++ source code.
  4. In Visual Studio Professional 2019, click the Debug -> Attach to process .
  5. Leave Connection type set to Default .
  6. Leave Connection target set to my local machine name.
  7. Choose Native code for the Attach to: prompt as I only wish to debug my C++ sources.
  8. Type "python" in the search box.
  9. Select the python.exe to attach to.

The problem...

The problem is that the last step shows more than one Python process (usually 3), all of which have been started by VS Code. Only one of them is the one I'm looking for (and will hit my C++ breakpoints), but I'm left to trial and error trying to find out which PID I'm after. VS Code does not tell me the relevant PID to attach to, and Visual Studio offers no additional details about the processes.

Workaround...

If I open up Process Explorer (from the Command Palette in VS Code), I am able to see the command line command that started the process as well as the process tree. From there, I'm able to guess the appropriate PID to attach to (ie the one where the --adapter-access-token option was supplied on the command line).

Is there an easier way to determine the PID to attach to?

Thanks!

It looks like VS Code (as of version 1.70.3) does not show the PID in the debug windows (eg in the call stack window).

Simpler workaround

I've developed a simpler workaround for anyone interested.

  1. Create a VS Code task that will run a shell script (PowerShell in this case) to output the Python process ID of interest. See code snippets, below.
  2. Launch the task as you normally would, eg select Tasks: Run Test Task from the VS Code command palette.

My project's .vscode/tasks.json :

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Get target process ID",
            "type": "shell",
            "windows": {
                "command": "PowerShell",
                "args": [
                    "-File",
                    "${workspaceFolder}\\.vscode\\target_pid.ps1",
                ],
                "options": {
                    "shell": {
                        "executable": "PowerShell.exe",
                    },
                },
            },
            "group": "test",
            "presentation": {
                "reveal": "always",
                "panel": "new",
            }
        }
    ]
}

The PowerShell script (in .vscode/target_pid.ps1 ):

Write-Information -MessageData "Locating python PIDs that can be attached to..." -InformationAction Continue

Get-WmiObject Win32_Process -Filter "name='python.exe' AND CommandLine LIKE '%--adapter-access-token%'" | select ProcessId, Path

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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