pythondebuggingvisual-studio-codepdbnode-inspector

Python --inspect-brk analogue


There's a --inspect-brk CLI switch awailable in Node.js to pause script execution until debugger is attached to the process. Is there a similar way I can tell Python interpreter to hang on until I attach a debugger?

I know about the PDB's "import pdb; pdb.set_trace()" option. The point is that I want to leverage VSCode built-in debugger in a non-hacky way. So far I've been able to run Python scripts with a "-m pdb" option, attach to it by PID with VSCode, pause the script execution with VSCode debugger before I type "next" into the pdb (somehow this sequence intercepts the control to the VSCode debugger) and I'm in. I know about the option of running the VSCode debugger directly by configuring the launch.json file but it doesn't suite the case where I run the Python script as part of some other shell script. The aforementioned Node.js switch is really helpful in such cases and I wonder if there's anything similar I can do with Python.


Solution

  • I know this is a bit of a thread necro but to answer your initial question of a "analog of node --inspect-brk for Python" is no. That functionality is not built into the python interpreter. My google searches a few days ago landed me on your unanswered question so I thought I'd come back and share what I have working.

    The closest experience I have found comes from the Command Line Debugging section of the VS Code documentation. I wont go into full details on the various command options that you can use but I will include what I have from my launch in case the link goes dead in the future.

    Assumptions:

    VS Code Launch Config

    {
      "name": "Attach To DebugPy (9339)",
      "type": "python",
      "request": "attach",
      "connect": {
        "port": 9339
      }
    }
    

    Work Flow:

    1. python3 -m debugpy --listen 9339 --wait-for-client ./main.py
    2. click the debugger "attach" target in VS Code