Convert Figma logo to code with AI

microsoft logodebugpy

An implementation of the Debug Adapter Protocol for Python

2,131
168
2,131
204

Top Related Projects

Python extension for Visual Studio Code

1,382

pdb++, a drop-in replacement for pdb (the Python debugger)

1,923

Integration of IPython pdb

Quick Overview

debugpy is a debugger for Python code, designed to be used with development tools and IDEs. It's an implementation of the Debug Adapter Protocol for Python, allowing for remote debugging and integration with various development environments.

Pros

  • Supports remote debugging, enabling debugging of Python code running on different machines or in containers
  • Implements the Debug Adapter Protocol, making it compatible with various IDEs and development tools
  • Provides advanced features like conditional breakpoints and logpoints
  • Actively maintained by Microsoft, ensuring regular updates and support

Cons

  • May have a steeper learning curve compared to simpler debuggers
  • Can be more resource-intensive than built-in Python debuggers
  • Requires additional setup and configuration for remote debugging scenarios
  • Some features may be IDE-dependent, limiting functionality in certain environments

Code Examples

  1. Attaching debugpy to a running Python process:
import debugpy

# Allow other computers to attach to debugpy at this IP address and port.
debugpy.listen(("0.0.0.0", 5678))

# Pause the program until a remote debugger is attached
debugpy.wait_for_client()
  1. Setting a breakpoint programmatically:
import debugpy

# Set a breakpoint at this line
debugpy.breakpoint()

# Your code continues here
print("This line will be reached after the debugger attaches")
  1. Launching a Python script with debugpy:
import debugpy

# Start debugging a Python script
debugpy.run("path/to/your/script.py")

Getting Started

To use debugpy in your Python project:

  1. Install debugpy:

    pip install debugpy
    
  2. Import debugpy in your Python script:

    import debugpy
    
  3. Configure your IDE or development tool to use debugpy as the debug adapter.

  4. Set breakpoints in your code or use debugpy.breakpoint() for programmatic breakpoints.

  5. Run your Python script with debugging enabled through your IDE or by using debugpy.run().

Competitor Comparisons

Python extension for Visual Studio Code

Pros of vscode-python

  • Provides a comprehensive Python development environment within VS Code
  • Offers features beyond debugging, including IntelliSense, linting, and formatting
  • Integrates seamlessly with other VS Code extensions and features

Cons of vscode-python

  • Larger and more complex codebase, potentially leading to slower updates
  • May include features not needed by all users, increasing resource usage
  • Requires VS Code as the development environment

Code Comparison

debugpy:

import debugpy

debugpy.listen(5678)
debugpy.wait_for_client()

vscode-python:

{
    "type": "python",
    "request": "launch",
    "name": "Python: Current File",
    "program": "${file}",
    "console": "integratedTerminal"
}

The debugpy example shows direct usage of the debugging library, while the vscode-python snippet demonstrates a launch configuration for debugging within VS Code.

1,382

pdb++, a drop-in replacement for pdb (the Python debugger)

Pros of pdbpp

  • Enhanced interactive debugging experience with syntax highlighting and tab completion
  • Lightweight and easy to install as a Python package
  • Extends the built-in pdb debugger, maintaining familiarity for users

Cons of pdbpp

  • Limited integration with IDEs and development environments
  • Lacks some advanced features like remote debugging and multi-threaded debugging support
  • Smaller community and less frequent updates compared to debugpy

Code Comparison

pdbpp:

from pdbpp import set_trace
set_trace()
# Debugging starts here

debugpy:

import debugpy
debugpy.listen(5678)
debugpy.wait_for_client()
# Debugging starts here

Key Differences

  • pdbpp focuses on enhancing the command-line debugging experience, while debugpy is designed for integration with IDEs and remote debugging scenarios
  • debugpy offers more robust features for complex debugging scenarios, including multi-threaded and remote debugging
  • pdbpp provides a more user-friendly interface for interactive debugging sessions, with features like syntax highlighting and tab completion

Both tools serve different use cases, with pdbpp being more suitable for quick, interactive debugging sessions, and debugpy excelling in integrated development environments and complex debugging scenarios.

1,923

Integration of IPython pdb

Pros of ipdb

  • Simpler and more lightweight, focusing on interactive debugging
  • Integrates seamlessly with IPython for enhanced interactive features
  • Easier to use for developers familiar with Python's built-in pdb debugger

Cons of ipdb

  • Less feature-rich compared to debugpy's comprehensive debugging capabilities
  • Limited remote debugging support
  • Lacks some advanced features like logpoints and conditional breakpoints

Code Comparison

ipdb:

import ipdb
ipdb.set_trace()
# Your code here

debugpy:

import debugpy
debugpy.listen(5678)
debugpy.wait_for_client()
# Your code here

Key Differences

  • debugpy is designed for remote debugging and integrates well with VS Code
  • ipdb focuses on enhancing the interactive debugging experience within Python
  • debugpy offers more advanced features like multi-threaded debugging and custom launch configurations
  • ipdb provides a more familiar interface for developers used to pdb
  • debugpy supports debugging Python scripts, modules, and packages, while ipdb is primarily for interactive debugging

Both tools have their strengths, with debugpy being more suitable for complex debugging scenarios and IDE integration, while ipdb excels in interactive and quick debugging sessions, especially for developers comfortable with pdb-style debugging.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

debugpy - a debugger for Python

An implementation of the Debug Adapter Protocol for Python 3.

Build Status GitHub PyPI PyPI

Coverage

OSCoverage
WindowsAzure DevOps coverage
LinuxAzure DevOps coverage
MacAzure DevOps coverage

debugpy CLI Usage

For full details, see the Command Line Reference.

Debugging a script file

To run a script file with debugging enabled, but without waiting for the client to attach (i.e. code starts executing immediately):

-m debugpy --listen localhost:5678 myfile.py

To wait until the client attaches before running your code, use the --wait-for-client switch.

-m debugpy --listen localhost:5678 --wait-for-client myfile.py

The hostname passed to --listen specifies the interface on which the debug adapter will be listening for connections from DAP clients. It can be omitted, with only the port number specified:

-m debugpy --listen 5678 ...

in which case the default interface is 127.0.0.1.

To be able to attach from another machine, make sure that the adapter is listening on a public interface - using 0.0.0.0 will make it listen on all available interfaces:

-m debugpy --listen 0.0.0.0:5678 myfile.py

This should only be done on secure networks, since anyone who can connect to the specified port can then execute arbitrary code within the debugged process.

To pass arguments to the script, just specify them after the filename. This works the same as with Python itself - everything up to the filename is processed by debugpy, but everything after that becomes sys.argv of the running process.

Debugging a module

To run a module, use the -m switch instead of filename:

-m debugpy --listen localhost:5678 -m mymodule

Same as with scripts, command line arguments can be passed to the module by specifying them after the module name. All other debugpy switches work identically in this mode; in particular, --wait-for-client can be used to block execution until the client attaches.

Attaching to a running process by ID

The following command injects the debugger into a process with a given PID that is running Python code. Once the command returns, a debugpy server is running within the process, as if that process was launched via -m debugpy itself.

-m debugpy --listen localhost:5678 --pid 12345

Ignoring subprocesses

The following command will ignore subprocesses started by the debugged process.

-m debugpy --listen localhost:5678 --pid 12345 --configure-subProcess False

debugpy Import usage

For full details, see the API reference.

Enabling debugging

At the beginning of your script, import debugpy, and call debugpy.listen() to start the debug adapter, passing a (host, port) tuple as the first argument.

import debugpy
debugpy.listen(("localhost", 5678))
...

As with the --listen command line switch, hostname can be omitted, and defaults to "127.0.0.1":

debugpy.listen(5678)
...

Waiting for the client to attach

Use the debugpy.wait_for_client() function to block program execution until the client is attached.

import debugpy
debugpy.listen(5678)
debugpy.wait_for_client()  # blocks execution until client is attached
...

breakpoint() function

Where available, debugpy supports the standard breakpoint() function for programmatic breakpoints. Use debugpy.breakpoint() function to get the same behavior when breakpoint() handler installed by debugpy is overridden by another handler. If the debugger is attached when either of these functions is invoked, it will pause execution on the calling line, as if it had a breakpoint set. If there's no client attached, the functions do nothing, and the code continues to execute normally.

import debugpy
debugpy.listen(...)

while True:
    ...
    breakpoint()  # or debugpy.breakpoint()
    ...

Debugger logging

To enable debugger internal logging via CLI, the --log-to switch can be used:

-m debugpy --log-to path/to/logs ...

When using the API, the same can be done with debugpy.log_to():

debugpy.log_to('path/to/logs')
debugpy.listen(...)

In both cases, the environment variable DEBUGPY_LOG_DIR can also be set to the same effect.

When logging is enabled, debugpy will create several log files with names matching debugpy*.log in the specified directory, corresponding to different components of the debugger. When subprocess debugging is enabled, separate logs are created for every subprocess.