Debugpy

1.8.20 · active · verified Sat Mar 28

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

Install

Imports

Quickstart

This quickstart demonstrates how to integrate `debugpy` into a Python script to enable remote debugging. It sets up a debug server, optionally waits for a client to attach (controlled by `WAIT_FOR_DEBUGGER` environment variable), and includes a programmatic breakpoint. To run: 1. Save as `app.py`. 2. On the remote machine (or local for testing), run `python app.py`. For remote access, ensure `DEBUG_HOST` is set to '0.0.0.0'. 3. In your IDE (e.g., VS Code), configure a 'Python: Remote Attach' launch configuration targeting the specified host and port (default 127.0.0.1:5678). Then start the debugger in your IDE.

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)

view raw JSON →