Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IPython - set a breakpoint in imported file

One can set a breakpoint in IPython + pdb like this:

run -d -b 150 file1.py 

That would break the execution of file1.py at line 150. Now, how can one set a break point in a file that is being called by file1.py? Something like the following:

run -d -b file2.py:106 file1.py

where file2.py is imported and called inside file1.py.

Many thanks.

like image 342
gozzilli Avatar asked Jan 13 '13 16:01

gozzilli


People also ask

How do you insert a breakpoint in Python?

You can insert a breakpoint with the breakpoint() function at any position in your code . This is new in Python 3.7, and is equivalent to the older import pdb; pdb. set_trace() command. # my_script.py a = int(0.1) b = 3.0 c = a + b breakpoint() # a lot of more code here...

Can I put a breakpoint in Jupyter notebook?

Set the breakpoints in the selected cell and press Alt + Shift + Enter for Windows or ⌥⇧↩ for macOS. Alternatively, you can right-click the cell and select Debug Cell from the context menu. The Jupyter Notebook Debugger tool window opens. Debugging is performed within a single code cell.

How do I set break point in pdb?

Optionally, you can also tell pdb to break only when a certain condition is true. Use the command b (break) to set a breakpoint. You can specify a line number or a function name where execution is stopped. If filename: is not specified before the line number lineno , then the current source file is used.

How do I debug with IPython?

In IPython, perhaps the most convenient interface to debugging is the %debug magic command. If you call it after hitting an exception, it will automatically open an interactive debugging prompt at the point of the exception.


1 Answers

One option which you might find workable would be to make file1.py into an IPython script, that is, change the name to file1.ipy, and then, instead of

import file2

do

%run -d -b 106 file2.py

I realize this might not be ideal as it requires editing file1.py.

edit: This would indeed be a useful feature in the %run command. I have added it here: https://github.com/ellbur/ipython

Also if you have a solution using pdb you might want to post that as an answer as well.

like image 108
Owen Avatar answered Oct 14 '22 09:10

Owen