[ACCEPTED]-How to detect that Python code is being executed through the debugger?-debugging
Python debuggers (as well as profilers and 8 coverage tools) use the sys.settrace
function (in the 7 sys
module) to register a callback that gets 6 called when interesting events happen.
If 5 you're using Python 2.6, you can call sys.gettrace()
to 4 get the current trace callback function. If 3 it's not None
then you can assume you should 2 be passing debug parameters to the JVM.
It's 1 not clear how you could do this pre 2.6.
Other alternative if you're using Pydev 1 that also works in a multithreading is:
try:
import pydevd
DEBUGGING = True
except ImportError:
DEBUGGING = False
A solution working with Python 2.4 (it should 5 work with any version superior to 2.1) and 4 Pydev:
import inspect
def isdebugging():
for frame in inspect.stack():
if frame[1].endswith("pydevd.py"):
return True
return False
The same should work with pdb by simply 3 replacing pydevd.py
with pdb.py
. As do3cc suggested, it 2 tries to find the debugger within the stack 1 of the caller.
Useful links:
Another way to do it hinges on how your 8 python interpreter is started. It requires 7 you start Python using -O for production 6 and with no -O for debugging. So it does 5 require an external discipline that might 4 be hard to maintain .. but then again it 3 might fit your processes perfectly.
From 2 the python docs (see "Built-in Constants" here or 1 here):
__debug__
This constant is true if Python was not started with an -O option.
Usage would be something like:
if __debug__:
print 'Python started without optimization'
If you're using Pydev, you can detect it 1 in such way:
import sys
if 'pydevd' in sys.modules:
print "Debugger"
else:
print "commandline"
From taking a quick look at the pdb docs 6 and source code, it doesn't look like there 5 is a built in way to do this. I suggest 4 that you set an environment variable that 3 indicates debugging is in progress and have 2 your application respond to that.
$ USING_PDB=1 pdb yourprog.py
Then in 1 yourprog.py:
import os
if os.environ.get('USING_PDB'):
# debugging actions
pass
You can try to peek into your stacktrace.
https://docs.python.org/library/inspect.html#the-interpreter-stack
when 3 you try this in a debugger session:
import inspect
inspect.getouterframes(inspect.currentframe()
you will 2 get a list of framerecords and can peek 1 for any frames that refer to the pdb file.
I found a cleaner way to do it,
Just add 2 the following line in your manage.py
#!/usr/bin/env python
import os
import sys
if __debug__:
sys.path.append('/path/to/views.py')
if __name__ == "__main__":
....
Then it would 1 automatically add it when you are debugging.
Since the original question doesn't specifically 2 call out Python2 - This is to confirm @babbageclunk's 1 suggested usage of sys also works in python3:
from sys import gettrace as sys_gettrace
DEBUG = sys_gettrace() is not None
print("debugger? %s" % DEBUG)
In my perllib, I use this check:
if 'pdb' in sys.modules:
# We are being debugged
It assumes the 1 user doesn't otherwise import pdb
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.