10

If I'm in debug mode, I want to do other stuff than when I'm not.

if DEBUG:
    STORED_DATA_FILE = os.path.join(TEMP_DIR, 'store.dat')
    LOG_LEVEL = logging.DEBUG
    print "debug mode"
else:
    STORED_DATA_FILE = os.path.join(SCRIPT_PATH, 'store.dat')
    LOG_LEVEL = logging.INFO
    print "not debug mode"

then:

python script.py
not debug mode

python -d script.py
debug mode

How can I detect that? It certainly isn't using the __debug__ variable.

user132262
  • 1,837
  • 4
  • 16
  • 17
  • Well to clarify for the questions below. All I really want to do is pick up some information from the general environment, but I tried setting an environment variable and looking for it in os.environ but that does not always work. – user132262 Oct 20 '09 at 09:48
  • unrelated to `-d` option: [Python: How to detect debug interpreter](http://stackoverflow.com/questions/646518/python-how-to-detect-debug-interpreter) – jfs Nov 12 '12 at 22:47
  • http://stackoverflow.com/questions/27748132/is-there-a-flag-i-can-check-in-my-code-to-see-if-pycharms-debugger-is-running?answertab=active#tab-top This answer work for me. – Billy Sep 15 '15 at 08:07

2 Answers2

14

you can use python -O with the __debug__ variable

where -O means optimise. so __debug__ is false

-d turns on debugging for the parser, which is not what you want

John La Rooy
  • 295,403
  • 53
  • 369
  • 502
8

Parser debug mode is enabled with -d commandline option or PYTHONDEBUG environment variable and starting from python 2.6 is reflected in sys.flags.debug. But are you sure this is what you are looking for?

Denis Otkidach
  • 32,032
  • 8
  • 79
  • 100