Zen of Debugging in Python - Part 4
You dont need to install anything, just use the docker! and you are ready to debug remotely!

Try it here
Python scratchpad
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 debugpyOnce 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 You can also integrate debugpy directly into your Python code, allowing your application to start the debugger programmatically. Here’s a simple example: 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. Attachable debuggers like debugpy offer several benefits: 🐳 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! 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.From Your Code
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.")
Why Use Attachable Debuggers?
Conclusion
