Connect Visual Studio 2019 debugger to Python process in VS Code

Viewed 38

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 (i.e. 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!

1 Answers

It looks like VS Code (as of version 1.70.3) does not show the PID in the debug windows (e.g. 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, e.g. 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 "`r`nIdentifying PIDs that are viable attachement targets..." -InformationAction Continue

Write-Information -MessageData "`nUse one of the following PIDs if Jupyter is not involved:" -InformationAction Continue
Get-WmiObject Win32_Process -Filter "name='python.exe' AND CommandLine LIKE '%--adapter-access-token%'" | select ProcessId, Path

Write-Information -MessageData "`nUse one of the following PIDs if Jupyter is involoved:" -InformationAction Continue
Get-WmiObject Win32_Process -Filter "name='python.exe' AND CommandLine LIKE '%ipykernel_launcher%'" | select ProcessId, Path
Related