I'm new to ipython and I am trying to use ipython to debug my code. I did:
[1]: %pdb
Automatic pdb calling has been turned ON
and then
In [2]: %run mycode.py
and in the code, I have 1/0 so it raises an exception and will automatically goes into the debug session.
ZeroDivisionError: float division
ipdb> variable
array([ 0.00704313, -1.34700666, -2.81474391])
So I can access variables. But when I do the following:
ipdb> b = variable
*** The specified object '= variable' is not a function or was not found along sys.path.
But this works:
ipdb> b = self.X
IPython has another way to start a debugger. You don't need to modify the source code of any file as we did before. If you run the %run -d filename.py magic command, IPython will execute the filename.py file and put a breakpoint on the first line there. It's just as if you would put the import ipdb; ipdb.
b
is used to set break points. So whatever follows b
is expected to be a function or line number.
If you type ipdb> help
you will see the full list of commands (reserved words).
You could use, say, x
or y
as a variable:
ipdb> y = variable
or
ipdb> exec 'b = variable'
I think you need to use '!' (pdb documentation): ! statement Execute the (one-line) statement in the context of the current stack frame. The exclamation point can be omitted unless the first word of the statement resembles a debugger command. To set a global variable, you can prefix the assignment command with a global statement on the same line, e.g.:
global list_options; list_options = ['-l']
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