Skip to content

Latest commit

 

History

History
173 lines (113 loc) · 6.25 KB

python-debugpy.md

File metadata and controls

173 lines (113 loc) · 6.25 KB

Python (debugpy) DAP server

To debug Python files, you can use the Debugpy DAP server.

Let’s debug the following test.py file:

def main():
    user = "hello world"
    print(user)

if __name__ == "__main__":
    main()

Set Breakpoint

Configure DAP server

  1. install Python. After that open a terminal and type python: Python command

  2. Use python3 -m pip install debugpy (python -m pip install debugpy on Windows) to install the required debugpy package for the Python that the template's check and launch command use, before you start your program with debugpy as below. The installer of the template runs this command too when debugpy is missing, but only when a debug session starts, which is after you started your program with debugpy. It needs python3 (python on Windows) on the PATH of your IDE.

  3. Create a test file named test.py with a simple Python script that includes a main() function printing "hello world"

def main():
    user = "hello world"
    print(user)

if __name__ == "__main__":
    main()
  1. Create a DAP Run/Debug configuration:

    DAP Configuration Type

  2. In the Server tab, click on create a new server:

    Create a new server

  3. It opens a new dialog to create DAP server: click Choose template... and select the Python - Debugpy template: Select Template

  4. After clicking on OK button, it will select the new server and pre-fill configurations:

Select New server

The command, python3 -m debugpy.adapter (python -m debugpy.adapter on Windows), starts the DAP server of Debugpy for the launch configuration. The attach configuration doesn't use it: it connects to a program which Debugpy already runs.

  1. Enable DAP server traces

If you wish to show DAP request/response traces when you will debug:

Show DAP traces

you need to select Trace with verbose.

Set verbose traces

Configure file mappings

To allows settings breakpoints to Python files, you need configure mappings in the Mappings tab. As you have selected Python - Debugpy server, it will automatically populate the file mappings like this:

File mappings

Launch the Python file

To start the Python file from the debug session, select Launch in the Configuration tab, and the Launch Python file configuration:

{
  "name": "Launch Python file",
  "type": "python",
  "request": "launch",
  "program": "${file}",
  "cwd": "${workspaceFolder}",
  "console": "internalConsole",
  "justMyCode": true
}

The DAP server, started with the command, runs the File of the Configuration tab with the Python that runs Debugpy, and its output goes to the console of the debug session. To run it with another Python, as the one of a virtual environment, set its path as the python of the configuration, or start the command with it.

Configure the Python file to run/debug with attach

As you have selected Python - Debugpy server, it will automatically populate the Attach configuration like this:

DAP Configuration/Configuration

  1. Attach as Debug mode should be selected.
  2. The DAP parameters of the launch should look like this:
{
   "name": "Attach",
   "type": "python",
   "request": "attach",
   "redirectOutput": true,
   "connect": {
      "host": "127.0.0.1",
      "port": 5678
   }
}

Address input is pre-filled with $connect.host and Port input is pre-filled with $connect.port which means that JSON configuration is used to retrieve the address and the port. You can see on the right of the inputs the real value of the address and port:

Attach fields preview

If you don't want to use JSON configuration, you can fill directly the real value:

Attach fields forced

The pre-filled configuration uses "redirectOutput": true to show output (like print(....)) in the IntelliJ console. Read Python debugging in VS Code for more information.

Set Breakpoint

After applying the run configuration, you should set a breakpoint to files which matches file mappings. Set a breakpoint in the test.py file:

Set Breakpoint

If your IDE has its own Python debugger (for example PyCharm), clicking the gutter creates a Python Line Breakpoint instead of a DAP Breakpoint. This breakpoint is hit as it is, you don't need a second DAP Breakpoint on the same line. See Forwarding native line breakpoints.

Start Python program in a Terminal

Open an IntelliJ Terminal (or other terminal), navigate to the directory containing your test file and execute the following command to start the debug server:

python -m debugpy --wait-for-client --listen 0.0.0.0:5678 test.py

This will make the script wait for a debugger to attach.

Start program

Debugging

Before starting the configuration, check that your address and port are correct:

Start program and attach

You can start the run configuration in either Run or Debug mode. Once started, you should see DAP traces in the console:

Debugging / Console

You will also see Threads and Variables:

Debugging / Threads

As Debugpy can support completion, you should benefit it:

Completion