Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect Python Is Running In Visual Studio Code

There are cases where code needs to act differently if running in Visual Studio Code.

Does anybody know the most efficient way to detect that the python code is running in the Visual Studio Code debugger?

So far, the best way I could find was using:

import sys
if 'debugpy' in sys.modules:
    print("Running in VS Code")
like image 573
Timothy C. Quinn Avatar asked Jul 28 '26 11:07

Timothy C. Quinn


1 Answers

On vscode the environment variable TERM_PROGRAM is set to 'vscode', you can check it by:

import os
if 'TERM_PROGRAM' in os.environ.keys() and os.environ['TERM_PROGRAM'] == 'vscode':
    print("Running in VS Code")
like image 165
Eran Avatar answered Jul 31 '26 01:07

Eran