[英]How to view variables in parent scope while using VS Code Python Debug Console
When I set a breakpoint in a function that is imported by another script, I would like to be able to view the variables that are in the parent scope from within the VS Code Python Debug Console.当我在另一个脚本导入的函数中设置断点时,我希望能够从 VS Code Python 调试控制台中查看父范围内的变量。 Is this possible, perhaps by modifying
launch.json
?这可能吗,也许通过修改
launch.json
?
Reproducible example:可重现的例子:
I have two files, scratch_script.py
and scratch_function.py
.我有两个文件,
scratch_script.py
和scratch_function.py
。 They exist in the same directory.它们存在于同一目录中。
I place breakpoints where indicated by comments below.我在下面的评论指示的地方放置断点。
Contents of scratch_script.py
: scratch_script.py
的内容:
import scratch_function
parent_scope_var = 'I disappear'
scratch_function.scratch_function() # breakpoint on this line
pass # breakpoint on this line
Contents of scratch_function.py
: scratch_function.py
的内容:
def scratch_function():
child_scope_var = 'I appear'
pass # breakpoint on this line
I run VS Code Python Debugger from scratch_script.py
.我从
scratch_script.py
运行 VS Code Python 调试器。
At the first break point, I can view parent_scope_var
in the Python Debug Console, but not child_scope_var
.在第一个断点,我可以在 Python 调试控制台中查看
parent_scope_var
,但不能child_scope_var
。 So far so good.到目前为止,一切都很好。
When I proceed to the second break point, I can view child_scope_var
, which is great.当我进入第二个断点时,我可以查看
child_scope_var
,这很棒。 But now I cannot view parent_scope_var
:但现在我无法查看
parent_scope_var
:
NameError: name 'parent_scope_var' is not defined
On the third breakpoint, the parent_scope_var
is seen again in the Python Debug Console, but not the child_scope_var
... Which is as I would expect.在第三个断点上,在 Python 调试控制台中再次看到了
parent_scope_var
,但没有看到child_scope_var
...这正如我所期望的那样。
So, my only confusion is, why can't I view parent_scope_var
when pausing at breakpoints in the called function?所以,我唯一的困惑是,为什么在被调用函数的断点处暂停时不能查看
parent_scope_var
? The variable still exists, right?变量仍然存在,对吗? Just in the parent scope?
只是在父范围内?
Yes, because when you debug to the second breakpoint, python executes the calling function.是的,因为当你调试到第二个断点时,python 会执行调用函数。 At this point, python will open the
scratch_function.py
file.此时,python会打开
scratch_function.py
文件。
First breakpoint:第一个断点:
Second breakpoint:第二个断点:
it will only display the variables that exist in the current file, and there is no parent_scope_var
variable in the scratch_function.py
file, the python interpreter cannot find parent_scope_var
, so the watch panel prompts an error: NameError: name 'parent_scope_var ' is not defined
.它只会显示当前文件中存在的变量,并且在
scratch_function.py
文件中没有parent_scope_var
变量,python解释器找不到parent_scope_var
,所以watch面板提示错误: NameError: name 'parent_scope_var ' is not defined
.
But as you might guess, the variable still exists at this point, just inside the scratch_script.py
file.但正如您可能猜到的那样,此时该变量仍然存在,就在
scratch_script.py
文件中。 At this point, you can open the scratch_script.py
file, hover the mouse over the parent_scope_var
variable, and vscode will clearly show you the variable value at this time.此时可以打开
scratch_script.py
文件,将鼠标悬停在parent_scope_var
变量上,此时vscode会清晰的显示变量值。
On a side note, you can also see the previous parent_scope_var
variable in the CALL STACK panel.另外,您还可以在CALL STACK面板中看到之前的
parent_scope_var
变量。
PS: a setting to display variable values inline in code files when debugging: PS:调试时在代码文件中内联显示变量值的设置:
//file:settings.json
{
"debug.inlineValues": "on",
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.