On this page:
Basics
PyCharm enables you to evaluate expressions and code fragments in the context of a stack frame currently selected in the Frames pane of the Debug window. In addition to regular expressions, you can also evaluate operator expressions, lambda expressions and anonymous classes.
The following evaluation modes are available:
- Expression Mode for evaluating single-line expressions.
-
Code Fragment Mode for evaluating short code portions. You can evaluate declarations,
assignments, loops and
if/else
.
Besides that, PyCharm provides a way to quickly evaluate an expression in the editor at caret or a selection.
While using the Expression Evaluation feature, be aware of the following:
- A method can be invoked within the Expression Evaluation dialog only if the debugger has stopped at a breakpoint, but has not been paused.
- Expression Evaluation can only be "single-level". In other words, if PyCharm stops at a breakpoint within a method called from the Expression Evaluation, you cannot use the Expression Evaluation feature again.
In the Frames pane, select the thread where you want an expression to be evaluated. Invoke the Evaluate Expression command in one of the following ways: On the main menu, choose Run | Evaluate Expression On the context menu of the editor, choose Evaluate Expression Press Click on the stepping toolbar of the Debug tool window Select an evaluation mode. If you want to evaluate a code fragment, click the Code Fragment Mode button. Depending on the selected mode, type the expression or statements to evaluate in the text field and click Evaluate. If the specified expression cannot be evaluated, the possible reason will be briefly described in the Result pane of the dialog box. If you have assigned a label to a variable, object, or watch, you can reference it by this label as if it were a local variable <label-name>_DebugLabel defined in the same context where the expression is evaluated. PyCharm also displays this label in the completion suggestion list.
- In the Frames pane, select the thread where you want an expression to be evaluated.
-
Invoke the Evaluate Expression command in one of the following ways:
- On the main menu, choose
- On the context menu of the editor, choose
- Press Alt+F8
-
Click
on the stepping toolbar of the Debug tool window
- Select an evaluation mode. If you want to evaluate a code fragment, click the Code Fragment Mode button.
-
Depending on the selected mode, type the expression or statements to evaluate in the text field and click Evaluate.
If the specified expression cannot be evaluated, the possible reason will be briefly described in the Result pane of the dialog box.
If you have assigned a label to a variable, object, or watch, you can reference it by this label as if it were a local variable
<label-name>_DebugLabel
defined in the same context where the expression is evaluated. PyCharm also displays this label in the completion suggestion list.
Evaluating Arbitrary Expressions
- Open the Evaluate Expression dialog box in one of the following ways:
- Choose on the main menu.
- PressAlt+F8.
- To have a specific variable evaluated, select it on the Variables pane, then choose or pressAlt+F8.
- In the
Evaluate Expression
dialog box, specify the expression to evaluate. Do one of the following:
-
In the Expression field, type the expression in question or choose one of the previously evaluated expressions
from the drop-down list.
If you have selected a specific variable on the Variables pane, this variable will be displayed in the Expression text box.
- To evaluate a code fragment, click the Code Fragment Mode button and
fill in the Code Fragment text box.
To return to the original mode, click the Expression mode button.
-
In the Expression field, type the expression in question or choose one of the previously evaluated expressions
from the drop-down list.
- Click the Evaluate button. The Result read-only field shows the evaluation output. If the specified expression cannot be evaluated, the Result field explains the reason.
Evaluating Expressions in the Editor
Note that during a debugger session, the value of any expression is shown in the tooltip every time
you hover your mouse pointer over it. If an expression contains children, clicking
expands the node and displays all children.
However, Quick evaluate expression helps you view the expression value using the keyboard only.
There are two ways to quickly evaluate an expression:
-
By using the Show Value On Selection Change
functionality:
-
In the Debug tool window toolbar click the
Settings icon
and select the Show Value On Selection Change option from the popup menu.
-
Select a code fragment with the mouse, or by clicking
Ctrl+W
. A tooltip with the expression value automatically appears under the selection and changes each time you change the selection:
-
In the Debug tool window toolbar click the
Settings icon
-
By invoking the tooltip with the expression value manually:
- Place the caret at the desired location, or select an expression to be evaluated.
-
Press Ctrl+Alt+F8. The tooltip with the expression value
appears under
the selected expression.