Python学习与开发4——使用 REPL 交互窗口
适用于 Python 的 Visual Studio 交互窗口提供丰富的“读取–求值–打印-循环”(REPL) 体验,极大地缩短了常用的“编辑-生成-调试”周期。The Visual Studio interactive window for Python provides a rich read-evaluate-print-loop (REPL) experience that greatly shortens the usual edit-build-debug cycle. 该交互窗口提供 Python 命令行 REPL 体验的所有功能。The interactive window provides all the capabilities of the REPL experience of the Python command line. 通过该窗口还可以非常轻松地与 Visual Studio 编辑器中的源文件交换代码,免去了使用命令行时的繁琐操作。It also makes it very easy to exchange code with source files in the Visual Studio editor, which is otherwise cumbersome with the command line.
-
通过以下方式打开交互窗口:在解决方案资源管理器中右键单击项目的 Python 环境(比如先前图片中显示的“Python 3.6(32 位)”),选择“打开交互窗口”。Open the interactive window by right-clicking the project's Python environment in Solution Explorer (such as "Python 3.6 (32-bit)" shown in an earlier graphic) and selecting Open Interactive Window. 也可以从 Visual Studio 主菜单选择“视图”>“其他窗口”>“Python 交互窗口”。You can alternately select View > Other Windows > Python Interactive Windows from the main Visual Studio menu.
-
交互窗口在编辑器下方打开,其中显示常用的
>>>Python REPL 提示符。The interactive window opens below the editor with the usual>>>Python REPL prompt. 用户通常想要放大交互窗口,这一点可通过拖动两个窗口之间的分隔条实现:Oftentimes you want to make the interactive window larger, which you can do by dragging the separator between the two windows:![Python 交互窗口以及通过拖动重设大小]()
提示
可通过拖动边界分隔条重设 Visual Studio 中所有窗口的大小。You can resize all of the windows in Visual Studio by dragging the bordering separators. 还可以将窗口拖出 Visual Studio 框架,以及在框架内按喜欢的方式重新排列窗口。You can also drag windows out independently of the Visual Studio frame, and rearrange them however you like within the frame. 有关完整的详细信息,请参阅自定义窗口布局。For complete details, see Customizing window layouts.
-
输入几个语句(如
print("Hello, Visual Studio"))和表达式(如123/456)以查看即时结果:Enter a few statements likeprint("Hello, Visual Studio")and expressions like123/456to see immediate results:![Python 交互窗口即时结果]()
-
开始编写多行语句(如函数定义)时,交互窗口将针对后续行显示 Python 的
...提示符,它与命令行 REPL 不同,可以提供自动缩进:When you start writing a multiline statement, like a function definition, the interactive window shows Python's...prompt for continuing lines, which, unlike the command-line REPL, provides automatic indentation:![含语句后续符的 Python 交互窗口]()
-
交互窗口提供所有已输入内容的完整历史记录,并通过多行历史记录项改进了命令行 REPL。The interactive window provides a full history of everything you've entered, and improves upon the command-line REPL with multiline history items. 例如,可以轻松地将
f函数的整个定义重新调用为单个单元,并轻松地将名称更改为make_double,而不用逐行重新创建函数。For example, you can easily recall the entire definition of theffunction as a single unit and easily change the name tomake_double, rather than re-creating the function line by line. -
Visual Studio 可从编辑器窗口向交互窗口发送多行代码。Visual Studio can 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 select parts of it to the interactive window. 然后就可以在快速 REPL 环境中使用此类代码片段,而不必运行整个程序。You can then work with such code fragments in the rapid REPL environment rather than having to run the whole program. 若要查看此功能,应先将
PythonApplication1.py文件中的for循环替换为以下代码:To see this feature, first replace theforloop in thePythonApplication1.pyfile with the following:Python# 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' -
仅选择
.py文件中的import和from语句,单击右键,选择“发送到交互窗口”(或按 Ctrl+Enter)。Select only theimportandfromstatements in the.pyfile, right-click, and select Send to Interactive (or press Ctrl+Enter). 代码片段会立即粘贴至交互窗口并运行。The code fragment is immediately pasted into the interactive window and run. 现在选择make_dot_string函数,重复相同的命令,即再次运行该代码片段。Now select themake_dot_stringfunction and repeat the same command, which again runs that code fragment. 由于代码定义了一个函数,因此可通过数次调用它来快速测试该函数:Because the code defines a function, you can quickly test that function by calling it a few times:![将代码发送到交互窗口并测试它]()
提示
如果在未选择任何内容的情况下,在编辑器中使用 Ctrl+Enter,则会在交互窗口中运行当前代码行,并自动在下一行放置插入点。Using Ctrl+Enter in the editor without a selection runs the current line of code in the interactive window and automatically places the caret on the next line. 利用此功能,通过反复按 Ctrl+Enter 即可便捷地单步调试代码,仅使用 Python 命令行则无法做到这一点。With this feature, pressing Ctrl+Enter repeatedly provides a convenient way to step through your code that is not possible with only the Python command line. 它还允许在不运行调试器的情况下单步调试代码,而且不必从头开始启动程序。It also lets you step through your code without running the debugger and without necessarily starting your program from the beginning.
-
还可以将任何源中的多行代码复制并粘贴到交互窗口,比如下面的代码片段,使用 Python 命令行 REPL 则难以实现此操作。You can also copy and paste multiple lines of code into the interactive window from any source, such as the snippet below, which is difficult to do with the Python command-line REPL. 粘贴后,交互窗口会运行该代码,就像直接在窗口中键入的一样:When pasted, the interactive window runs that code as if you'd typed it in:
Pythonfor i in range(360): s = make_dot_string(i) print(s)![使用“发送到交互窗口”粘贴多行代码]()
-
如用户所见,此代码运行正常,但其输出不太乐观。As you can see, this code works fine but its output isn't very inspiring.
for循环中的另一个单步执行值会显示更多余弦波。A different step value in theforloop would show more of the cosine wave. 幸运的是,由于整个for循环在 REPL 历史记录中作为单个单元,因此可轻易返回并进行任何所需的更改,然后再次测试函数。Fortunately, because the entireforloop is in the REPL history as a single unit, it's easy to go back and make whatever changes you want and then test the function again. 按向上键先重新调用for循环。Press the up arrow to first recall theforloop. 然后按向左键或向右键开始在代码中导航(在执行此操作前,向上键和向下键继续循环访问历史记录)。Then press the left or right arrows to start navigating in the code (until you do so, the up and down arrows continue to cycle through the history). 导航到range规范并将其更改为range(0, 360, 12)。Navigate to and change therangespecification torange(0, 360, 12). 然后按 Ctrl+Enter(在代码中的任意位置)再次运行整个语句:Then press Ctrl+Enter (anywhere in the code) to run the whole statement again:![在交互窗口中编辑以前的语句]()
-
反复尝试使用不同的单步执行设置,直到找到最喜欢的值。Repeat the process to experiment with different step settings until you find a value you like best. 也可以延长范围(例如
range(0, 1800, 12))使波形重复。You can also make the wave repeat by lengthening the range, for example,range(0, 1800, 12). -
如果对在交互窗口中编写的代码感到满意,可选择代码,单击右键,选择“复制代码”(Ctrl+Shift+C),然后粘贴到编辑器中。When you're satisfied with code you're written in the interactive window, select it, right-click and select Copy Code (Ctrl+Shift+C), and then paste into the editor. 注意 Visual Studio 的这一特殊功能如何自动省略所有输出以及
>>>和...提示符。Notice how this special feature of Visual Studio automatically omits any output as well as the>>>and...prompts. 例如,下图演示如何对包含提示符和输出的选定内容使用“复制代码”命令:For example, the image below shows using the Copy Code command on a selection that includes prompts and output:![交互窗口中用于包含提示符和输出的选定内容的“复制代码”命令]()
粘贴到编辑器时,仅得到以下代码:When you paste into the editor, you get only the code:
Pythonfor i in range(0, 1800, 12): s = make_dot_string(i) print(s)若想复制交互窗口的准确内容,包括提示符和输出,只需使用标准的“复制”命令。If you want to copy the exact contents of the interactive window, including prompts and output, just use the standard Copy command.
-
刚刚已经了解如何使用交互窗口的快速 REPL 环境处理一小段代码的细节问题,然后将该代码轻松添加到项目的源文件。What you've just done is use the rapid REPL environment of the interactive window to work out the details for a small piece of code, then you conveniently added that code to your project's source file. 如果现在使用 Ctrl+F5(或“调试”>“开始执行(不调试)”)再次运行代码,则会看到想要的准确结果。








浙公网安备 33010602011771号