2009-10-20 22 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

相關問題