Skip to content

Latest commit

 

History

History
142 lines (87 loc) · 4.74 KB

File metadata and controls

142 lines (87 loc) · 4.74 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 pip install debugpy to install the required debugpy package.

  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, select Python- Debugpy template: Select Template

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

Select New server

As Debugpy DAP server is consumed with attach request, the command is empty.

  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

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

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