2009-10-20 19 views
8

如果我处于调试模式,我想要做其他的事情,而不是当我不在时。如何检查是否从脚本中设置了python调试选项

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" 

则:

python script.py 
not debug mode 

python -d script.py 
debug mode 

如何检测呢?它当然不使用__debug__变量。

+0

那么,以澄清以下问题。我真正想做的就是从一般环境中获取一些信息,但我尝试设置一个环境变量并在os.environ中查找它,但这并不总是奏效。 – user132262 2009-10-20 09:48:40

+0

与'-d'选项无关:[Python:How to detect debug interpreter](http://stackoverflow.com/questions/646518/python-how-to-detect-debug-interpreter) – jfs 2012-11-12 22:47:24

+0

http:// stackoverflow。 com/questions/27748132/is-there-a-flag-i-can-check-in-my-code-see-if-pycharms-debugger-is-running?answertab = active#tab-top 这个答案适合我。 – 2015-09-15 08:07:36

回答

6

分析器调试模式与-d命令行选项或PYTHONDEBUG环境变量启用和Python 2.6中开始体现在sys.flags.debug什么。但是你确定这是你正在寻找的?

+0

这适用于2.6及以上版本,谢谢。现在如果任何人有答案为2.5及以下? – user132262 2009-10-20 09:56:49

12

可以使用python -O__debug__可变

其中-O手段优化。所以__debug__是假

-d打开调试解析器,这是不是你想要

+0

是的,-O连续监视脚本,甚至不需要重新加载 – igaurav 2014-11-15 01:21:21

相关问题