34
votes

Say I have an IPython session, from which I call some script:

> run my_script.py

Is there a way to induce a breakpoint in my_script.py from which I can inspect my workspace from IPython?

I remember reading that in previous versions of IPython one could do:

from IPython.Debugger import Tracer;     

def my_function():
    x = 5
    Tracer()
    print 5;

but the submodule Debugger does not seem to be available anymore.

Assuming that I have an IPython session open already: how can I stop my program a location of my choice and inspect my workspace with IPython?

In general, I would prefer solutions that do not require me to pre-specify line numbers, since I would like to possibly have more than one such call to Tracer() above and not have to keep track of the line numbers where they are.

6
Out of curiosity, why would you ever want more than one tracer at once? At that point it seems like you should be doing logging, not debugging. - Wilduck
@Wilduck: I would like to inspect my workspace under certain circumstances, and those circumstances may occur in different locations in my code. - Amelio Vazquez-Reina
Okay, maybe I phrased my question poorly. I understand why you would want to look at different parts of your code. Why would you want to have those tracers in your code at the same time? If you're not using the debugger to permanently fix a bug (i.e. one bug at a time) wouldn't it be better/easier to set up logging to automatically collect information about how your program is running in various states? I'm asking because it seems that you're trying to use debugging tools for something logging takes care of. - Wilduck
Thanks @Wilduck. I am not really interested in logging. I would like to mathematically and interactively examine the value of several variables when certain conditions are met. I could certainly dump the variables to disk and inspect them later, but that would not allow me to resume execution, which sometimes I want. This type of workflow is common and powerful in scientific computing (e.g. MATLAB supports this through the use of keyboard statements) and it allows one to inspect and visualize data interactively without fully disrupting or having to reinitialize a program. - Amelio Vazquez-Reina
I see, that does make sense. I'll leave my answer up, since it might be useful to someone stumbling on this question later, but I'm afraid I don't have any further suggestions for you personally. Good luck. - Wilduck

6 Answers

32
votes

The Tracer() still exists in ipython in a different module. You can do the following:

from IPython.core.debugger import Tracer

def my_function():
    x = 5
    Tracer()()
    print 5

Note the additional call parentheses around Tracer

edit: For IPython 6 onwards Tracer is deprecated so you should use set_trace() instead:

from IPython.core.debugger import set_trace

def my_function():
    x = 5
    set_trace()
    print 5
21
votes

You can run it and set a breakpoint at a given line with:

run -d -b12 myscript

Where -b12 sets a breakpoint at line 12. When you enter this line, you'll immediately drop into pdb, and you'll need to enter c to execute up to that breakpoint.

13
votes

This is the version using the set_trace() method instead of the deprecated Tracer() one.

from IPython.core.debugger import Pdb

def my_function():
    x = 5
    Pdb().set_trace()
    print 5
8
votes

Inside the IPython shell, you can do

from IPython.core.debugger import Pdb
pdb = Pdb()
pdb.runcall(my_function)

for example, or do the normal pdb.set_trace() inside your function.

5
votes

I have always had the same question and the best workaround I have found which is pretty hackey is to add a line that will break my code, like so:

...
a = 1+2
STOP
...

Then when I run that code it will break, and I can do %debug to go there and inspect. You can also turn on %pdb to always go to point where your code breaks but this can be bothersome if you don't want to inspect everywhere and everytime your code breaks. I would love a more elegant solution.

2
votes

With Python 3 (v3.7+), there's the new breakpoint() function. You can modify it's behaviour so it'll call ipython's debugger for you.

Basically you can set an environment variable that points to a debugger function. (If you don't set the variable, breakpoint() defaults to calling pdb.)

To set breakpoint() to call ipython's debugger, set the environment variable (in your shell) like so:

# for bash/zsh users
export PYTHONBREAKPOINT='IPython.core.debugger.set_trace'
# powershell users
$env:PYTHONBREAKPOINT='IPython.core.debugger.set_trace'

(Note, obviously if you want to permanently set the environment variable, you'll need to modify your shell profile or system preferences.)

You can write:

def my_function():
    x = 5
    breakpoint()
    print 5;

And it'll break into ipython's debugger for you. I think it's handier than having to import from IPython.core.debugger import set_trace and call set_trace().