How To Attach Debugger To Python Script Called From Terminal In Visual Studio Code

Pavol Kutaj
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 of script.py
  • Verify that it connects (e.g. no error, breakpoints work)
  • TERMINAL: Press Enter to continue and the debugger should stop at the breakpoint.

2. LINKSπŸ”—πŸ”—πŸ”—πŸ”—

--

--

Pavol Kutaj

Today I Learnt | Infrastructure Support Engineer at snowplow.io with a passion for cloud infrastructure/terraform/python/docs. More at https://pavol.kutaj.com