Reputation: 45
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")
Upvotes: 4
Views: 1245
Reputation: 1723
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.
Upvotes: 2
Reputation: 856
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")
Upvotes: 1