Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to detect programmatically spyder debug run #23523

Closed
joo0ne opened this issue Jan 21, 2025 · 2 comments
Closed

how to detect programmatically spyder debug run #23523

joo0ne opened this issue Jan 21, 2025 · 2 comments

Comments

@joo0ne
Copy link

joo0ne commented Jan 21, 2025

I need to programmatically detect that the code is running in debug mode. Something like #ifdef DEBUG in C. This is needed for additional code that will improve debugging. For example, for CUDA, you need to set the environment variable CUDA_LAUNCH_BLOCKING=1 , etc.

I have already checked the advice from the Internet (+AI):

  • pdb._getframe().f_back is not None - does not work;

  • os.getenv('SPYDER_DEBUG_MODE') - does not work;

  • 'ipdb' in sys.modules - does not work.

@ccordoba12
Copy link
Member

ccordoba12 commented Jan 21, 2025

Hey @joo0ne, thanks for reporting. This should do the trick:

from IPython import get_ipython

shell = get_ipython()
shell.is_debugging()

That's True when Spyder is in debugging mode, False otherwise.

Let us know if that works for you.

@joo0ne
Copy link
Author

joo0ne commented Jan 22, 2025

Unfortunately it doesn't work for me. But I found a solution on SO that works:

import sys

gettrace = getattr(sys, 'gettrace', None)

if gettrace and gettrace():
    print('Debug')
else:
    print("No debugger")

@joo0ne joo0ne closed this as completed Jan 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants