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.
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...
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With