ptvsd (Python Tools for Visual Studio Debugger)

4.3.2 · deprecated · verified Thu Apr 16

ptvsd is a remote debugging server for Python, primarily used to enable debugging capabilities in development environments like Visual Studio and Visual Studio Code. While its last stable release was 4.3.2 in August 2019, it has largely been superseded by `debugpy`, which is based on ptvsd's development branch. All future development for Microsoft's Python debugger is happening in `debugpy`.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize ptvsd for remote debugging within a Python script. It sets up a listening server that a debugger (e.g., Visual Studio Code) can connect to. It uses environment variables for configuration for better portability and includes an optional `wait_for_attach()` call to pause execution until the debugger connects. A `breakpoint()` call is included to show how to trigger a debugger pause during execution.

import ptvsd
import os

# Enable remote debugging. By default, listens on all interfaces (0.0.0.0) and port 5678.
# The 'address' can be configured via environment variables for flexibility.
# For a more secure setup, specify a host (e.g., '127.0.0.1') and/or a secret (ptvsd 3.x required it).

# Example using environment variables for host and port
DEBUG_HOST = os.environ.get('PTVSD_HOST', '0.0.0.0')
DEBUG_PORT = int(os.environ.get('PTVSD_PORT', 5678))

print(f"Waiting for debugger attach on {DEBUG_HOST}:{DEBUG_PORT}")
ptvsd.enable_attach(address=(DEBUG_HOST, DEBUG_PORT))

# Optional: Wait for the debugger to attach before continuing execution.
# This is useful when debugging script startup.
# Remove or comment out in production or if your debugger automatically attaches early.
ptvsd.wait_for_attach()

print("Debugger attached. Continuing execution...")

def my_function():
    x = 10
    y = 20
    # Use breakpoint() for Python 3.7+ or ptvsd.break_into_debugger() for older Python versions
    breakpoint() # This will pause execution if a debugger is attached
    z = x + y
    print(f"Result: {z}")

my_function()

view raw JSON →