joon
joon

Reputation: 4017

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

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

Upvotes: 7

Views: 8695

Answers (4)

liyuan
liyuan

Reputation: 553

In python3 you can use a single exclamation mark to override ipdb commands

!b

Upvotes: 3

seanv507
seanv507

Reputation: 1287

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']

Upvotes: 3

zvin
zvin

Reputation: 11

you can use

locals()["b"] = variable

Upvotes: 1

unutbu
unutbu

Reputation: 879093

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'

Upvotes: 9

Related Questions