Imagine the following scenario: a script is started from the IPython shell and at a break point the python debugger is called. Using the PDB commands one can analyze the code and variables at this point. But often it turns out that the values of the variables call for a deeper research.
Is it possible to export the value of a variable to the IPython shell?
My specific use case: I struggle with a quite huge numpy array which does not seem to have the correct values. I know that I can run any python commands from the python debugger, but it would be helpful to save the values of the variable at different break points and to use all of them at IPython shell. I am imaging something like
ipdb> global var1; var1 = var
ipdb> continue
...
ipdb> global var2; var2 = var
ipdb> continue
...
In [2]: abs(var1 - var2) # do some interesting calculations with IPython
Not a pretty solution, but working:
...
then
You can use globals():
This assumes the break point is set in
my_module.py
, so we are editing the globals of the modulemy_moule
.You need to distinguish different
globals()
.For example, suppose we have a module: mymodule.py
We run it under the control of
pdb
.At the beginning, namely, before executing
test()
, the environment in pdb is your currentglobals()
. Thusfoobar
is defined, whilefoo
is not defined.Then we execute
test()
and stop at the end ofbar = 200
The environment in pdb has been changed. It uses
mymodule
'sglobals()
intest()
. Thus 'foobaris not defined. while
foo` is defined.We have exported two variables
foobar2
andfoo2
. But they live in different scopes.You have already found the solution. But it works slightly differently.