4

I am using Visual Studio 12 with Python Tools for Visual Studio 1.5 to develop Python programs. That works very well.

Now I would like to check within the Python code whether we are running within Visual Studio or not. Is there an easy way to achieve this?

Pavel Minaev
  • 99,783
  • 25
  • 219
  • 289
Matthias
  • 9,817
  • 14
  • 66
  • 125
  • My first thought would be to set an environment variable as part of the run configuration and check for that... But that seems a bit kludgy and haven't used VS in ages now.... (2005 I think!?) – Jon Clements Jan 29 '13 at 17:00
  • There's no way to set an environment variable as part of run configuration in PTVS, unfortunately. – Pavel Minaev Aug 07 '13 at 02:24

1 Answers1

5
import sys
if '$visualstudio_py_debugger' in sys.modules:
    print("Running in Visual Studio")
else:
    print("Running outside Visual Studio")
Martin v. Löwis
  • 124,830
  • 17
  • 198
  • 235
  • Just keep in mind that the name of this module is an implementation detail and is subject to change in future versions. In particular, in PTVS 2.0, this code will not work if you're using mixed-mode Python/C debugging. – Pavel Minaev Aug 07 '13 at 02:25