Debugpy: A Powerful Debugger for Python Developers
Debugging is an essential part of the development process, and having a reliable and feature-rich debugger is crucial for Python developers. That’s where debugpy comes into play. debugpy is an implementation of the Debug Adapter Protocol (DAP) for Python 3, which provides a powerful and versatile debugger for Python developers.
Using the debugpy
CLI
The debugpy
CLI allows you to run script files, modules, and even attach to running processes for debugging. To run a script file with debugging enabled, you can use the following command:
console
python -m debugpy --listen localhost:5678 myfile.py
You can also wait until the client attaches before running your code by using the --wait-for-client
switch. This can be useful when you want to pause the execution until you connect your client.
console
python -m debugpy --listen localhost:5678 --wait-for-client myfile.py
In addition to script files, you can also debug modules using the -m
switch:
console
python -m debugpy --listen localhost:5678 -m mymodule
To attach to a running process by its ID, you can use the --pid
option:
console
python -m debugpy --listen localhost:5678 --pid 12345
Enabling Debugging with the debugpy
Import
If you prefer to enable debugging programmatically, you can import debugpy
and make use of its API. By calling debugpy.listen()
at the beginning of your script with a (host, port) tuple, you can start the debug adapter:
python
import debugpy
debugpy.listen(("localhost", 5678))
Alternatively, you can omit the host and use the default value of "127.0.0.1"
:
python
debugpy.listen(5678)
To wait for the client to attach, you can use the debugpy.wait_for_client()
function:
python
import debugpy
debugpy.listen(5678)
debugpy.wait_for_client()
Programmatic Breakpoints with breakpoint()
When debugging your code, you may want to set breakpoints programmatically. debugpy supports the standard breakpoint()
function, which can be used to pause execution on the calling line. If the debugger is attached, the code will pause as if it had a breakpoint set. If there’s no client attached, the function does nothing, and the code continues executing normally. Here’s an example:
“`python
import debugpy
debugpy.listen(…)
while True:
…
breakpoint()
…
“`
Debugger Logging
For troubleshooting purposes, debugpy provides logging capabilities to help you identify and resolve issues. You can enable debugger internal logging via the CLI using the --log-to
switch:
console
python -m debugpy --log-to path/to/logs ...
Similarly, you can enable logging when using the API:
python
import debugpy
debugpy.log_to('path/to/logs')
debugpy.listen(...)
When logging is enabled, debugpy will create log files with names matching debugpy*.log
in the specified directory. Separate logs are created for each component of the debugger, and when subprocess debugging is enabled, logs are created for every subprocess.
In conclusion, debugpy is a powerful debugger for Python developers, offering CLI and import usage for debugging script files, modules, and attaching to running processes. With features like programmatic breakpoints and logging, debugpy provides developers with a comprehensive toolset for efficient and effective debugging. Give debugpy a try and experience enhanced debugging capabilities in your Python projects.
Leave a Reply