Tutorial: Use the Interactive REPL window in Visual Studio
This article presents Step 3 in the tutorial series Work with Python in Visual Studio.
The Visual Studio interactive development environment (IDE) provides various windows that support different development tasks. Step 1 and Step 2 in the tutorial describe how to manage files in Solution Explorer and work with file content in the code editor. In Step 3, you use the Visual Studio Interactive Window for Python for a rich read-evaluate-print-loop (REPL) experience that greatly shortens the usual edit-build-debug cycle. By working in the Interactive Window for Python, you gain all the capabilities of the REPL experience of the Python command line. It also makes it easy to exchange code with source files in the Visual Studio editor, which is otherwise cumbersome with the command line.
In Step 3 of the tutorial, you learn how to:
- Access the Interactive Window for Python and set the Python interpreter
- Enter and run code in the window, and scroll the command history
- Insert existing code or code from other sources
- Copy code and program output from the Interactive Window
Prerequisites
A Python application project that has a Python file (.py) with code created in Step 2: Write and run Python code.
To best support the REPL experience, install the
ipython
andipykernel
packages. For more information, see Python environments packages tab.
Open the Interactive Window for Python
Follow these steps to open the Interactive Window and start working with code:
Use one of the following methods to open the Interactive Window for Python:
- In Solution Explorer, right-click your project node and select Open Interactive Window.
- In Solution Explorer, under the Python Environments node for your project, right-click the Python environment for your project and select Open Interactive Window. (An example of a Python environment is Python 3.11 (64-bit).)
- On the toolbar, select View > Other Windows > Python Interactive Windows or use the keyboard shortcut Alt+I.
Visual Studio opens the Interactive Window for Python under the editor window. The window has the standard >>> Python REPL prompt:
In the Interactive Window, you can use the Environment dropdown list to select a specific Python interpreter to work with:
You can make the Interactive Window larger by moving the separator between the window and the code editor:
Tip
You can resize any window in Visual Studio by moving the separators between the window and surrounding windows. You can also move windows out of the Visual Studio frame, and rearrange windows within the frame.
In the Interactive Window, enter a statement like
print("Hello, Visual Studio")
and a calculation expression like123/456
. Visual Studio displays immediate results for your code:Next, enter a multiline statement like the function definition shown in the following example.
As you enter the code, the Interactive Window shows the Python continuation prompt (... ). Unlike the command-line REPL experience, the Interactive Window provides automatic indentation as you enter code:
To add a new line in your code, select Shift+Enter.
Scroll through the list of commands you entered so far by using the Up/Down arrow keys.
The Interactive Window provides a full history of everything you enter, and improves upon the command-line REPL experience with multiline history items. You can quickly recall the entire definition of a function as a single unit and change the function name or adjust other statements. This approach is easier than re-creating the function line by line.
Reuse and modify existing code
Visual Studio provides different ways to use existing Python code in the Interactive Window, such as copy and paste, and accessing previously entered code as described earlier. Another method is to send multiple lines of code from an editor window to the Interactive Window. This capability allows you to maintain code in a source file and easily send the chosen fragments to the Interactive Window. You can then work with the code fragments in the rapid REPL environment rather than having to run the whole program.
Try these features by completing the following steps:
Open your Python project file (.py) in the editor. (Tutorial Step 2 explains how to create this file, where the default file name is PythonApplication1.py.)
Replace the
for
loop statement in the file with the following code:# Create a string with spaces proportional to a cosine of x in degrees def make_dot_string(x): return ' ' * int(20 * cos(radians(x)) + 20) + 'o'
Right-click the
import
statement in the file and select Send to Interactive (or the keyboard shortcut Ctrl+E). Repeat this process for thefrom
statement.When you select the Send to Interactive command, the selected code fragment is immediately pasted into the Interactive Window and run. In this case, Visual Studio imports the
cos
andradians
routines from themath
library, and also thesys
library.Repeat the process again for the
make_dot_string
function. Be sure to send both lines of the function. After you send the complete code for the function, enter a new line in the Interactive Window.Because this code defines a function, you can quickly test it by calling the function a few times, as shown in the following example:
Tip
You can use the keyboard shortcut Ctrl+Enter to run the current line of code in the Interactive Window. After the code runs, the caret prompt moves to the next line. Select Ctrl+Enter repeatedly for a convenient way to step through your code, which isn't possible in the Python command-line experience. You can also use this approach to step through your code without running the debugger and without necessarily starting your program from the beginning.
Try the process again, but this time copy and paste multiple lines of code into the Interactive Window from any source. You can use the following code snippet:
for i in range(360): s = make_dot_string(i) print(s)
The multiline paste action is difficult to do in the Python command-line REPL experience. After your paste the code, the Interactive Window runs the code as if you entered it directly in the window:
The current code works, but the output isn't inspiring. A different step value in the
for
loop can show more of the cosine wave. The code thefor
loop is available in the REPL history as a single unit. You can locate the code and make whatever changes you want and test the function again.Scroll through your command history by using the Up arrow key and stop at the
for
loop code.Use the Left/Right arrow keys to move into the code for the
for
loop statement. When you reach the beginning of a line, select the Left arrow key to move to the end of the previous line.In the
for
loop statement, go to therange
definition and change the statement torange(0, 360, 12)
.Select Ctrl+Enter anywhere in the
for
loop statement to run the function again:Repeat the process to experiment with different step settings until you find a value you like. You can also make the wave repeat by lengthening the range, for example,
range(0, 1800, 12)
.
Copy your code and program output
Visual Studio lets you copy any code that you write in the Interactive Window, and also any output from running your code.
Follow these steps to copy code from the Interactive Window to the editor:
In the Interactive Window, select the code you want to copy.
Right-click the selected code and choose Copy Code or use the keyboard shortcut Ctrl+Shift+C.
Paste the copied code into the editor. This special feature of Visual Studio automatically omits any output, along with the
>>>
and...
prompts, in the selection.When you paste into the editor, you get only the code:
for i in range(0, 1800, 12): s = make_dot_string(i) print(s)
To copy the exact contents of the Interactive Window including prompts and any program output, use the standard Copy command.
In these steps, you used the rapid REPL environment of the Interactive Window to work out the details for a small piece of Python code and conveniently added the code to your project's source file. When you rerun your code with Ctrl+F5 or Debug > Start without Debugging, you see the exact results you wanted.