23

I'm unable to find/execute the interactive mode in Visual Studio Code. What are the step-by-step instructions for it?

I don't have "Other windows".

Is there something I need to install?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Duck Flightsson
  • 341
  • 1
  • 3
  • 4

3 Answers3

8

If you type # %% in your Visual Studio Code editor while editing a .py file, then an interactive cell is created and it can be evaluated.

The nice part of this is that # denotes a comment in a .py file, so you can save and run your code as a script.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
usr0192
  • 313
  • 3
  • 9
6

If you have the Python extension, you can use the Python Interactive feature (this is a IPython/Jupyter console, which can run parts of your code as 'cells', i.e., snippets of code executed in one go).

This article writes about the interactive window and many more possibilities of using Jupyter in Visual Studio Code: Python Interactive window

Best option

When editing Python code, select something and press Shift + Enter. This will open the interactive window and run your selection as a single cell.

I have personally been annoyed many times by accidentally pressing Shift + Enter instead of Enter on a line of code which (alone) would be a syntax error. Then, one day I decided to start using this feature consciously.

Terminal fallback

Press: Ctrl + `

This opens a system shell (Bash, or on Windows: PowerShell or cmd.exe).

Run python, or ipython if you have it (pip install ipython), and use the interactive window. In the terminal, you can also execute your script, by typing python myscript.py arguments.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Tomasz Gandor
  • 8,235
  • 2
  • 60
  • 55
5

In Visual Studio Code, you can enter the Python interactive window in the following places:

Option 1

F1Python: Create Python Interactive Window

This is the Python interactive window that comes with Visual Studio Code. You can enter the interactive window without inputting instructions. Please use "Ctrl+Enter" to execute the code:

option1_screenshot

Option 2

Ctrl + Shift + `

This is the cmd window that comes with the Visual Studio Code integration computer. We need to enter the command "python" to enter the Python interactive window:

option2_screenshot

PS

For more information about using Python in Visual Studio Code, you can refer to the official Visual Studio Code documentation: Python in Visual Studio Code.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jill Cheng
  • 9,179
  • 1
  • 20
  • 25
  • 4
    I think they changed how all of that works since this answer was given. there is no `Create Python Interactive Window` anymore. `shift+enter` always works, but now they also have jupyter notebook cells with `# %%` – Christoph Rackwitz May 29 '22 at 18:00