简体   繁体   中英

Flag that indicates if Pycharm's is running the code with “Debug” or “Run”

I've been wanting to run something only when I use "Debug" in PyCharm and make the code avoid it when it is run using "Run".

I saw some references to the __debug__ variable but it doesn't seem to change value as long as I run my code in PyCharm. I've seen some other comments referring to -O which I think, refer to running the code outside of the IDE.

I'm looking at creating something like this

if variable:
    print("Debug mode")
else:
    print("Run mode")

I would see if sys.gettrace() would work, like this:

import sys

if sys.gettrace() is None:
    print("Run Mode")
else: print("Debug Mode")

The documentation on gettrace is HERE , and should work with most implementations/IDEs. I also use Pycharm (Community and Professional) and use this to separate debugging logic.

PyCharm's debugger is merged with PyDev's, so you can use:

import sys

if "pydevd" in sys.modules: 
    print("Debug mode")
else:
    print("Run mode")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM