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()-
install Python. After that open a terminal and type
python:
-
Use
python3 -m pip install debugpy(python -m pip install debugpyon Windows) to install the requireddebugpy packagefor 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 needspython3(pythonon Windows) on the PATH of your IDE. -
Create a test file named
test.pywith a simple Python script that includes amain()function printing"hello world"
def main():
user = "hello world"
print(user)
if __name__ == "__main__":
main()-
Create a DAP Run/Debug configuration:
-
In the
Servertab, click oncreate a new server: -
It opens a new dialog to create DAP server: click
Choose template...and select thePython - Debugpytemplate:
-
After clicking on
OKbutton, it will select the new server and pre-fill configurations:
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.
- Enable DAP server traces
If you wish to show DAP request/response traces when you will debug:
you need to select Trace with verbose.
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:
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.
As you have selected Python - Debugpy server, it will automatically populate the Attach configuration like this:
AttachasDebug modeshould be selected.- 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:
If you don't want to use JSON configuration, you can fill directly the real value:
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.
After applying the run configuration, you should set a breakpoint to files which matches file mappings.
Set a breakpoint in the test.py file:
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.
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.
Before starting the configuration, check that your address and port are correct:
You can start the run configuration in either Run or Debug mode. Once started, you should see DAP traces in the console:
You will also see Threads and Variables:
As Debugpy can support completion, you should benefit it:













