60

As I know ,I can view variables in VS Code by setting breakpoints, Is it possible to view and use the variables likes Spyder in Console?
Such as the picture posted bellow: the running result variable "line" can be viewed and used in Console tab. The variables in the script can be easily viewed and used in Console after running it.(also can be viewed in variable explorer)

So, Is there any way(by setting or extension) to view and use variables in VS Code other than setting breakpoints. Likes Spyder.

enter image description here

1

2 Answers 2

44

In VSCode, there are two ways to view script variables besides setting breakpoints:

  1. Open the "OUTLINE" column in the lower left corner of VSCode:

    enter image description here

  2. Right-click "Run Current File in Python Interactive Window" in the script, and select "Show variables active in jupyter kernel":

    enter image description here

Sign up to request clarification or add additional context in comments.

5 Comments

thanks for reply. for the 1 way, the variables are listed in "OUTLINE", but just variable name,no variable value, how to show the value in "OUTLINE"?
@KuMasann -Currently, VSCode only supports displaying variables and method names here, if you need to view the value of variables, you can use method 2.
is there still no other way of displaying variable values other than through option 2? It seems unnecessarily complicated to me to always have a second interactive window open. feels like data exploration is not very intuitively integrated in VS code?
The "Interactive Environment" leaves a python interpreter open in the background (alias kernel), like Spyder and Jupyter do. Spyder has the same additional window-notebook that shows the python kernel inputs and outputs. In Spyder, it can be moved around anywhere in the layout whereas, in VS Code, it's an editor window and can only be in the editor panel (not in the bottom panel like Terminal for example) - stackoverflow.com/questions/66921894/… , code.visualstudio.com/docs/python/jupyter-support-py
I think the issue is that you can't see the values for the interactive session when you hover over a variable in a code editor window. I think many people run "cell mode" (#%%) and you really need to see the interactive variables in the editor.
1
  1. The Panel area should have a tab called "JUPYTER: VARIABLES" that shows jupyter variables. You can focus that view by running Jupyter: Focus on Variables View in the command palette. There's also a button for it in the notebook toolbar (labelled "Variables"). You need to run the cells first for their variables to show up there. You can double-click a row in that table to open a data viewer. Related docs can be found here. The VS Code team also suggests alternative data-viewing extensions like ms-toolsai.datawrangler (related docs for that here).

  2. There's an experiment going on to add a Notebook Variables view to the Run/Debug View for Notebook editors: Explore built-in "variable explorer" notebook functionality #165445. Quoting the testing issue ticket:

    1. set "notebook.experimental.variablesView": true, in settings
    2. Create a notebook and run python cells that assign variables [...]
    3. Open the debug view and find the new Notebook Variables section
    4. explore the variables

    It's still in the experimental stage. There's a list of known issues/shortcomings in the testing issue ticket.

See also the issue tickets

I suggest that you give those issue tickets a look and a thumbs up to show support for them if they reflect the feature you want. You can also subscribe to them to get notified about discussion and progress. Please avoid making noisy comments there like ones that just consist of "+1" / "bump".

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.