Debugpy
debugpy is an implementation of the Debug Adapter Protocol (DAP) for Python, enabling powerful debugging capabilities for various Python applications, including scripts, web apps, and remote processes. It is primarily used to connect Integrated Development Environments (IDEs) like VS Code to a running Python process for features such as setting breakpoints, stepping through code, and inspecting variables. The library is actively maintained, with frequent updates to support new Python versions and fix issues. The current version is 1.8.20.
Warnings
- breaking When using VS Code, the `type` field in `launch.json` debug configurations for Python debugging has changed from `"python"` to `"debugpy"`. Older configurations using `"python"` will no longer work with recent versions of the Python Debugger extension.
- breaking debugpy replaced ptvsd as the default remote debugging library in Visual Studio and the Python extension for VS Code. Projects or tutorials referencing `ptvsd` APIs will need to be updated to use `debugpy`.
- gotcha Exposing the debug server to all network interfaces by calling `debugpy.listen(('0.0.0.0', port))` can pose a security risk. Anyone on the network who can connect to that port can execute arbitrary code within the debugged process.
- gotcha Code executed before `debugpy.listen()` (or before `python -m debugpy --listen...`) cannot be debugged. If you want to debug from the very beginning of your script, you must explicitly call `debugpy.wait_for_client()` immediately after `debugpy.listen()`.
- gotcha Debugging child processes can be tricky. By default, `debugpy` might not automatically inject itself into subprocesses. Recent versions include fixes, but explicit configuration may still be needed.
- gotcha When debugging remotely, incorrect `pathMappings` in your IDE's launch configuration can prevent breakpoints from being hit or source code from being correctly correlated.
- gotcha While debugpy itself supports Python 3.8+, issues have been reported with newer Python versions (e.g., 3.14) requiring specific debugpy fixes. Conversely, debugging older Python versions (e.g., 3.6, 3.7) with current VS Code Python extensions requires installing older, specific versions of the VS Code extensions.
Install
-
pip install debugpy
Imports
- debugpy
import debugpy
- debugpy.listen
debugpy.listen(("0.0.0.0", 5678)) - debugpy.wait_for_client
debugpy.wait_for_client()
Quickstart
import debugpy
import os
# Configuration from environment variables for flexibility
DEBUG_HOST = os.environ.get('DEBUG_HOST', '127.0.0.1')
DEBUG_PORT = int(os.environ.get('DEBUG_PORT', '5678'))
WAIT_FOR_DEBUGGER = os.environ.get('WAIT_FOR_DEBUGGER', 'true').lower() == 'true'
print(f"[*] Debug server attempting to listen on {DEBUG_HOST}:{DEBUG_PORT}")
try:
debugpy.listen((DEBUG_HOST, DEBUG_PORT))
print(f"[*] Debug server listening. Host: {DEBUG_HOST}, Port: {DEBUG_PORT}")
if WAIT_FOR_DEBUGGER:
print("[*] Waiting for debugger client to attach...")
debugpy.wait_for_client()
print("[*] Debugger attached. Continuing program execution.")
else:
print("[*] Not waiting for client. Program will continue.")
# Your application logic starts here
name = "World"
message = f"Hello, {name}! This is debugpy in action."
print(message)
# Example: A programmatic breakpoint
debugpy.breakpoint()
result = 10 * 2
print(f"Calculated result: {result}")
except Exception as e:
print(f"[ERROR] Failed to start debug server or an error occurred: {e}")
import sys
sys.exit(1)