2

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. Is this possible, perhaps by modifying launch.json?

Reproducible example:

I have two files, scratch_script.py and scratch_function.py. They exist in the same directory.

I place breakpoints where indicated by comments below.

Contents of 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:

def scratch_function():
    child_scope_var = 'I appear'
    pass # breakpoint on this line

I run VS Code Python Debugger from scratch_script.py.

At the first break point, I can view parent_scope_var in the Python Debug Console, but not child_scope_var. So far so good.

When I proceed to the second break point, I can view child_scope_var, which is great. But now I cannot view 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.

So, my only confusion is, why can't I view parent_scope_var when pausing at breakpoints in the called function? The variable still exists, right? Just in the parent scope?

2 Answers 2

3

Yes, because when you debug to the second breakpoint, python executes the calling function. At this point, python will open the scratch_function.py file.

First breakpoint:

enter image description here

Second breakpoint:

enter image description here

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.

enter image description here

But as you might guess, the variable still exists at this point, just inside the scratch_script.py file. 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.

enter image description here

On a side note, you can also see the previous parent_scope_var variable in the CALL STACK panel.

enter image description here

PS: a setting to display variable values inline in code files when debugging:

//file:settings.json
{
    "debug.inlineValues": "on",  
}
Sign up to request clarification or add additional context in comments.

Comments

0

Currently, you can switch different local varible environment by click call stack.

You need to add "justMyCode": false to your launch config.

See also vscode debug editor Variables can be inspected in the VARIABLES section of the Run and Debug view or by hovering over their source in the editor. Variable values and expression evaluation are relative to the selected stack frame in the CALL STACK section.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.