How To Attach Debugger To Python Script Called From Terminal In Visual Studio Code
2 min readMar 10, 2022
The aim of this pageπ is to note how a debugger can be attached to the process in VSCode and Python when you call a script from a shell. Note that you need a running Python program. This is a problem for simple scripts β my workaround is to add a manual pause within the runtime and attach a debugger only once the script is already running (and is paused)
I use this when I am quickly testing scripts that accept arguments directly from the command line.
1. STEPSππππ
script.py
: Add a pause in your script in the beginning by adding
input("Press Enter to continue...:")
script.py
: Set a breakpoint somewhere in the following sections of the script- TERMINAL: call
python script.py
and do not press Enter to continue. You need a running Python process to attach the debugger to. - VSCODE: Select View β Run β Add Configuration
- VSCODE: In Debugger select the following Python configuration
Python: Attach using Process ID
- VSCODE: From the dropdown, select the
python
the process containing the name ofscript.py
- Verify that it connects (e.g. no error, breakpoints work)
- TERMINAL: Press Enter to continue and the debugger should stop at the breakpoint.