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
pip install debugpyto install the requireddebugpy package. -
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, select
Python- Debugpytemplate:
-
After clicking on
OKbutton, it will select the new server and pre-fill configurations:
As Debugpy DAP server is consumed with attach request, the command is empty.
- 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:
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:
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:













