Is it possible to transfer a variable from a pdb session to the original interactive session?

I am using pdb to learn the script that called run -d in an ipython session. It would be useful to be able to display some variables, but I need them in the main ipython environment to do this.

So, I'm looking for a way to make a variable available back in the main interactive session after exiting pdb. If you set the variable in the top frame, it seems to be present in the ipython session, but this does not work for any frames.

Something like export in the following:

 ipdb> myvar = [1,2,3] ipdb> p myvar [1, 2, 3] ipdb> export myvar ipdb> q In [66]: myvar Out[66]: [1, 2, 3] 
+2
source share
1 answer

In ipython docs as well as run? command run? from the ipython prompt,

after execution, IPython interactive namespace is updated with all variables defined in the program (except __name__ and sys.argv)

In the section "Defined in the program" (slightly inaccurate use of terms), this does not mean "anywhere inside any nested functions found there" - it means "in the globals() the script / module you run ning. If you are in any nesting, globals()['myvar'] = [1,2,3] will still work just fine, just like your reliable export if it existed.

Change If you are in another module, you need to set the name in the global list of the source file - after import sys , if necessary, sys.modules["originalmodule"].myvar = [1, 2, 3] will do what you want.

+2
source

All Articles