Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

accessing variables in the debugging session with ipython and %pdb on

Tags:

python

ipython

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
like image 295
joon Avatar asked Nov 15 '10 17:11

joon


People also ask

How do I use IPython for debugging?

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.


2 Answers

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'
like image 67
unutbu Avatar answered Sep 22 '22 08:09

unutbu


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']
like image 29
seanv507 Avatar answered Sep 21 '22 08:09

seanv507