Attach debugger(debugpy
) for flexible, non-intrusive debugging without altering code.
Attaching a debugger to a Python application offers a flexible and powerful method for debugging without the need to directly modify the source code. This approach is particularly useful when working with running applications or when debugging in environments where altering code is not feasible. Among the various tools available for Python,
debugpy
stands out for its versatility and ease of use in attaching to running processes. Here’s a closer look at how you can utilize debugpy
for non-intrusive debugging from both the command line and within your code.← this item is applicable for steps 1, 2 of debugging
What is debugpy
?
debugpy
is an open-source debugger for Python, designed to be used with Visual Studio Code but also available for use in other environments. It supports a wide range of Python versions and can be used for remote debugging, making it a versatile choice for developers.Attaching debugpy
for Debugging
From the Command Line
To use
debugpy
from the command line, you first need to install the package if you haven't done so already. You can install debugpy
using pip:pip install debugpy
Once installed, you can start a debug server and attach it to your running Python application. Here’s how to initiate
debugpy
and attach it to a process with a specific PID (Process ID):debugpy --listen localhost:5678 --pid <PID>
This command tells
debugpy
to listen for debugger connections on localhost
port 5678
and attach to the process with the given PID. You'll need to replace <PID>
with the actual process ID of your Python application.From Your Code
You can also integrate
debugpy
directly into your Python code, allowing your application to start the debugger programmatically. Here’s a simple example:TheTechCruise.com Pyodide Terminal
import debugpy
# Start a debug server listening on a specific port
debugpy.listen(('localhost', 5678))
# Wait for a client (debugger) to attach
debugpy.wait_for_client()
# Now your code is ready to be debugged
print("Debugging is now enabled.")
In this example,
debugpy.listen()
starts a debug server that listens for a connection from a debugger on localhost
port 5678
. The debugpy.wait_for_client()
call then pauses execution until a debugger attaches, ensuring that you don't miss any part of the execution you wish to debug.Why Use Attachable Debuggers?
Attachable debuggers like
debugpy
offer several benefits:- Non-intrusive: They allow debugging without altering the original source code, which is ideal for production environments or third-party libraries.
- Flexibility: You can attach to and detach from a running process at any time, providing flexibility in debugging sessions.
- Remote Debugging:
debugpy
supports remote debugging, enabling you to debug code running on a different machine.
This is particularly helpful if your application is containerized, just override the
CMD
and you should be able to debug outside the container. In case of k8s, you can also run a sidecar and expose the debugger as well!
Conclusion
Using attachable debuggers such as
debugpy
provides a powerful and flexible approach to debugging Python applications. Whether you choose to initiate debugging from the command line or directly within your code, debugpy
offers a seamless experience that enhances your development workflow. By integrating debugpy
into your debugging practices, you can enjoy the benefits of non-intrusive debugging with the capability to tackle complex debugging scenarios more effectively.