Using REPL in Visual Studio

In this section, we will explore the usage of the standard read–eval–print loop (REPL) tool for Python inside Visual Studio. As mentioned in the introduction, PTVS has an enhanced version of REPL. Besides the standard Python commands in the REPL version of PTVS—the Interactive Python window—there are some added commands and functionalities that can help speed up the debugging process and also enable simple testing of your code.

The enhanced commands are shown by typing $help in REPL, as shown in the following screenshot:

Using REPL in Visual Studio

We'll go into detail on the most used and interesting commands. The $cls command cleans up the command line, while the $reset command cleans up the engine in a way that you can restart with a clean REPL environment.

The most interesting functions are $load and $mod. The $load command permits you to execute the content of a given Python file inside REPL:

Using REPL in Visual Studio

An example of the $load function in the REPL tool

These functions are useful as they provide an on-the-fly view of the execution cycle of your code.

The $mod command gives you the opportunity to change the scope on which the REPL tool is operating.

When REPL starts, it's automatically set on the main module of the opened project, (__main__). Typing $mod followed by the name of the module allows you to switch to another module of the same project, giving access to the module and its content.

Using REPL in Visual Studio

As we can see in the preceding screenshot, the user interface of the REPL window has a combobox at the top, which allows us to interactively switch the scope. IntelliSense is also managed in REPL.

We can execute every piece of code in our project using the Send to Interactive command in the context menu under the coding panel (or, use the Ctrl + E shortcut):

Using REPL in Visual Studio

We can also instruct the debugger to use REPL as the output of the application by selecting the Execute Project in Python Interactive option from the DEBUG menu (or by using the Shift + Alt + F5 shortcut).

It's also possible to tell the debugger to use REPL as the output of the application instead of the standard console output; to do this, just click on the Execute Project in Python Interactive entry in the DEBUG menu or use the Shift + Alt + F5 shortcut:

Using REPL in Visual Studio

This is particularly useful when developing back-office modules in a web application for which the debugging and testing of the code is particularly difficult if you wish to do this directly in the browser. Using the REPL tool, you can achieve a much more productive and quicker process.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.198.94