0

There seem to be many, many threads here and Google hits re use of exit, quit, os._exit(), sys.exit() in Python, but none I've found play nicely with IPython.

When I use most of them I get:

UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.
  warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)

I'm trying to debug in Python - when a certain condition is met I want Python to stop executing and return to the Python command line so I can examine global variables, etc.

How to do that?

For now I've done this, which works when I hit Ctrl-C, but there ought to be a way that doesn't require the Ctrl-C.

def halt():
    while True:
        time.sleep(0.2)
1
  • Have you tried calling the python debugger? call pdb.set_trace() and examine your variables from the debugger. Commented Apr 15, 2021 at 17:08

1 Answer 1

1

Have you tried using the Python Debugger?

import pdb

while doing_something:
    if condition:
        pdb.set_trace()

If you want to manually control when the loop exits, you can try:

while doing_something:
    try:
        do_something()
    except KeyboardInterrupt:
        pdb.set_trace()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.