Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exiting Python Debugger ipdb

I use ipdb fairly often in a way to just jump to a piece of code that is isolated i.e. it is hard to write a real script that uses it. Instead I write a minimal test case with mocking and jump into it.

Exemplary for the workflow:

def func():    ...    import ipdb    ipdb.set_trace()    ...  def test_case():     ...     func()     ... 

Then, invoke

py.test test_file.py -s -k test_case 

Now, usually I just check one variable or two, and then want to quit. Change the code and do it over again.

How do I quit? The manual says q quits the debugger. It doesn't (really). You have to quit a few times before the debugger actually terminates. The same behavior for Ctrl-C and Ctrl-D (with the additional frustration that hitting Ctrl-D several times eventually quits the terminal, too).

Is there a smart way to force quit? Is this workflow even sensible? What is the standard way to do it?

like image 381
Joachim Avatar asked Aug 17 '15 16:08

Joachim


People also ask

How do I exit IPDB?

If you enter pdb interactive mode there is no way to return to ipdb or ipython. The proper way to exit is by using ctrl-d .

How do I close debugger in Python?

The Python debugger will automatically start over when it reaches the end of your program. Whenever you want to leave the pdb console, type the command quit or exit .

What does IPDB mean in Python?

ipdb, the IPython-enabled Python Debugger, is a third party interative debugger with all pdb's functionality and adds IPython support for the interactive shell, like tab completion, color support, magic functions and much more. You use ipdb just as you use pdb but with an enhanced user experience.

What is ipdb in Python?

ipdb, the IPython-enabled Python Debugger, is a third party interative debugger with all pdb’s functionality and adds IPython support for the interactive shell, like tab completion, color support, magic functions and much more. You use ipdb just as you use pdb but with an enhanced user experience.

How do I exit the debugger in IPython?

As mentioned in another answer, this was a bug in IPython 5.1. It was fixed in this pull request and is no longer an issue from IPython 5.2 and onwards. You can now use q, quit (), or Ctrl + d to exit the debugger.

What is the best way to debug code in Python?

Searching for a better approach to debug code, there is always something better available out there, I came across pdb and ipdb. pdb, the Python Debugger, is an interactive debugger that is part of the Python standard library.

How do I break into a Python debugger?

Insert the following code at the location where you want to break into the debugger: When the line above is executed, Python stops and waits for you to tell it what to do next. You’ll see a (Pdb) prompt. This means that you’re now paused in the interactive debugger and can enter a command.


2 Answers

I put the following in my .pdbrc

import os  alias kk os.system('kill -9 %d' % os.getpid()) 

kk kills the debugger and (the process that trigger the debugger).

like image 39
Joachim Avatar answered Sep 26 '22 18:09

Joachim


The following worked for me:

import sys sys.exit() 

On newer versions of ipython, as mentioned above and below, this doesn't work. In that case,

import os os._exit(0) 

should still do the trick.

like image 92
KDN Avatar answered Sep 22 '22 18:09

KDN