nerdfever.com
nerdfever.com

Reputation: 1782

Exit Python script to IPython command line for debugging

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)

Upvotes: 0

Views: 97

Answers (1)

Mike McCartin
Mike McCartin

Reputation: 179

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()

Upvotes: 1

Related Questions