Part 5: Debug Configurations and Production Debugging

Introduction

You know how to debug with F5, but production systems need more: debugging specific modules, attaching to running processes, debugging tests, remote debugging, and configurations for different environments. I learned this debugging a microservice in production—couldn't reproduce the bug locally, had to attach the debugger to the live container.

This final article covers advanced debug configurations, debugging pytest tests, remote debugging, Docker container debugging, and techniques for investigating production issues.

Launch.json Configurations

The .vscode/launch.json file defines debug configurations for different scenarios.

Basic Structure

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "debugpy",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal"
        }
    ]
}

Common Configuration Options

Real-World Configurations

Configuration 1: FastAPI Application

Use case: Debug FastAPI endpoints with hot reload

How to use:

  1. Set breakpoints in your endpoint handlers

  2. Select "FastAPI: Debug Server" from debug dropdown

  3. Press F5

  4. Send requests to http://localhost:8000

Configuration 2: Flask Application

Configuration 3: Django Application

Configuration 4: Specific Script with Arguments

Configuration 5: Module with Entry Point

Debugging Tests

Pytest Configuration

Real test file:

Debug single test:

  1. Open test file

  2. Set breakpoint in test

  3. Select "Pytest: Current File"

  4. Press F5

Debug Specific Test Function

Debug All Tests

Real Example: Debugging Failing Test

Debug session:

Found floating-point precision issue! Fix:

Attach to Running Process

Debug Python code that's already running.

Configuration for Attaching

Prepare Code for Attach

Add debugpy to your application:

Steps to debug:

  1. Run application: python app.py

  2. Application waits at wait_for_client()

  3. In VS Code: Select "Python: Attach"

  4. Press F5

  5. Debugger connects, execution continues

Attach Without Waiting

Remote Debugging

Debug code running on another machine or in a container.

Remote Machine Setup

On remote machine:

Run on remote:

VS Code configuration:

Docker Container Debugging

Dockerfile:

docker-compose.yml:

VS Code launch.json:

Debug workflow:

Environment-Specific Configurations

Multiple Environments

Compound Configurations

Run multiple debug sessions simultaneously:

Select "Backend + Worker" to debug both simultaneously!

Production Debugging Strategies

Strategy 1: Structured Logging

Can't always attach debugger to production. Use structured logging:

Output:

Strategy 2: Distributed Tracing

For microservices, use OpenTelemetry:

Traces show full request flow across services.

Strategy 3: Debug Mode Flag

Enable debug mode in production when investigating specific issues.

Strategy 4: Error Tracking

Use Sentry or similar:

Sentry captures full context when errors occur.

Debugging Performance Issues

Configuration with Profiling

Using cProfile

Memory Profiling

Best Practices

1. Separate Configurations for Each Component

2. Use Environment Files

With input variable:

3. Document Your Configurations

4. Version Control launch.json

Include .vscode/launch.json in git so team members have consistent debug configurations.

Series Conclusion

You've completed the Code Debugging 101 series! You now know:

  • Part 1: Setting up VS Code debugger, basic breakpoints, debug interface

  • Part 2: Conditional breakpoints, logpoints, stepping through code

  • Part 3: Variables inspection, watch expressions, call stacks, debug console

  • Part 4: Debugging async code, exceptions, decorators, multi-threading

  • Part 5: Launch configurations, debugging tests, remote debugging, production strategies

From Print Debugging to Professional Debugging

You started with print() statements. Now you can:

  • Set strategic breakpoints that pause only when needed

  • Inspect complex data structures interactively

  • Debug async, threaded, and decorated code

  • Test fixes in debug console before changing code

  • Debug remote applications and containers

  • Investigate production issues systematically

Next Steps

  1. Practice daily: Use debugger instead of prints

  2. Create configurations: Build launch.json for your projects

  3. Share knowledge: Teach teammates debugging techniques

  4. Explore extensions: Try debugging extensions for your frameworks

  5. Read documentation: VS Code debugging docs have more advanced features

The Debugging Mindset

Debugging isn't just about fixing bugs—it's about understanding your code deeply. The debugger is a learning tool as much as a fixing tool. Use it to:

  • Explore unfamiliar codebases

  • Understand library behavior

  • Verify your assumptions

  • Learn how frameworks work internally

Happy debugging!


Based on debugging thousands of Python applications across local development, containers, remote servers, and production environments using VS Code.

Last updated