是否可以将pdb会话中的变量传递到原始的交互式会话中? [英] Is it possible to pass a variable out of a pdb session into the original interactive session?
问题描述
我正在使用pdb来检查在ipython会话中调用 run -d
的脚本。
可以绘制一些变量,但是我需要它们在主要的ipython环境中才能做到这一点。所以我正在寻找的是在退出pdb之后,在主要的交互式会话中使一个变量可用的方法。如果您在最上面的框架中设置了一个变量,它似乎在ipython会话中,但这对于任何进一步的框架都不起作用。
code> export 在以下内容中:
ipdb> myvar = [1,2,3]
ipdb> p myvar
[1,2,3]
ipdb> export myvar
ipdb> q
在[66]中:myvar
输出[66]:[1,2,3]
根据ipython的 docs ,还有一个运行?
命令从ipython提示符,
执行后,IPython
交互式命名空间使用程序中定义的所有变量更新
(除了__ name __
和sys.argv)
通过在程序中定义(稍微使用术语),并不意味着任何嵌套函数在运行脚本/模块的 globals()
中找到 - 这意味着。如果你处于任何嵌套状态, globals()['myvar'] = [1,2,3]
应该仍然可以正常工作,就像你希望的
修改:如果你是e在不同的模块中,您需要在原始的全局变量中设置名称 - 如果需要,在 import sys
之后, sys.modules [originalmodule]。myvar = [1,2,3]
会做你想要的。
I am using pdb to examine a script having called run -d
in an ipython session.
It would be useful to be able to plot some of the variables but I need them in the main ipython environment in order to do that.
So what I am looking for is some way to make a variable available back in the main interactive session after I quit pdb. If you set a variable in the topmost frame it does seem to be there in the ipython session, but this doesn't work for any frames further down.
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]
Per ipython's docs, and also a run?
command from the ipython prompt,
after execution, the IPython interactive namespace gets updated with all variables defined in the program (except for
__name__
and sys.argv)
By "defined in the program" (a slightly sloppy use of terms), it doesn't mean "anywhere within any nested functions found there" -- it means "in the globals()
of the script/module you're run
ning. If you're within any kind of nesting, globals()['myvar'] = [1,2,3]
should still work fine, just like your hoped-for export
would if it existed.
Edit: If you're in a different module, you need to set the name in the globals of your original one -- after an import sys
if needed, sys.modules["originalmodule"].myvar = [1, 2, 3]
will do what you desire.
这篇关于是否可以将pdb会话中的变量传递到原始的交互式会话中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!