import inspect

def isdebugging():
    for frame in inspect.stack():
        if frame[1].endswith('pydevd.py'):
            return True
return False
How to detect that Python code is being executed through the debugger?