python交互模式切换_Python 交互式窗口 (REPL) - Visual Studio | Microsoft Docs
使用 Python 交互窗口Work with the Python Interactive window
02/11/2019
本文內(nèi)容
Visual Studio 為每個(gè) Python 環(huán)境提供交互讀取-評(píng)估-打印-循環(huán) (REPL) 窗口,改進(jìn)了在命令行中運(yùn)行 python.exe 獲得的 REPL。Visual Studio provides an interactive read-evaluate-print loop (REPL) window for each of your Python environments, which improves upon the REPL you get with python.exe on the command line. 借助交互窗口(通過(guò)“視圖” > “其他窗口” > “<環(huán)境>交互”菜單命令打開(kāi)),可以輸入任意 Python 代碼,并查看即時(shí)結(jié)果。The Interactive window (opened with the View > Other Windows > Interactive menu commands) lets you enter arbitrary Python code and see immediate results. 這種編碼方式有助于了解與實(shí)驗(yàn) API 和庫(kù),并以交互方式開(kāi)發(fā)要包含在項(xiàng)目中的工作代碼。This way of coding helps you learn and experiment with APIs and libraries, and to interactively develop working code to include in your projects.
Visual Studio 有大量 Python REPL 模式可供選擇:Visual Studio has a number of Python REPL modes to choose from:
REPLREPL
描述Description
編輯Editing
調(diào)試Debugging
映像Images
標(biāo)準(zhǔn)Standard
默認(rèn) REPL,直接與 Python 通信Default REPL, talks to Python directly
標(biāo)準(zhǔn)編輯(多行等)。Standard editing (multiline, etc.).
是,通過(guò) $attachYes, via $attach
否No
調(diào)試Debug
默認(rèn) REPL,與已調(diào)試的 Python 進(jìn)程通信Default REPL, talks to debugged Python process
標(biāo)準(zhǔn)編輯Standard editing
僅調(diào)試Only debugging
否No
IPythonIPython
REPL 與 IPython 后端通信REPL talks to IPython backend
IPython 命令,Pylab 的便利IPython commands, Pylab conveniences
否No
是,在 REPL 中內(nèi)聯(lián)Yes, inline in REPL
帶 Pylab 的 IPythonIPython w/o Pylab
REPL 與 IPython 后端通信REPL talks to IPython backend
標(biāo)準(zhǔn) IPythonStandard IPython
否No
是,單獨(dú)窗口Yes, separate window
本文介紹標(biāo)準(zhǔn) REPL 模式和調(diào)試 REPL 模式。This article describes the Standard and Debug REPL modes. 有關(guān) IPython 模式的詳細(xì)信息,請(qǐng)參閱使用 IPython REPL。For details on IPython modes, see Use the IPython REPL.
有關(guān)包含示例的詳細(xì)演練,包括與編輯器的交互(如 Ctrl+Enter),請(qǐng)參閱教程步驟 3 :使用交互 REPL 窗口。For a detailed walkthrough with examples, including the interactions with the editor such as Ctrl+Enter, see Tutorial Step 3: Use the Interactive REPL window.
打開(kāi)交互窗口Open an Interactive window
以下有幾種方法可用于針對(duì)某個(gè)環(huán)境打開(kāi)交互窗口。There are several ways to open the Interactive window for an environment.
方法一,切換到 Python 環(huán)境窗口(“視圖” > “其他窗口” > “Python 環(huán)境”或 Ctrl+K > Ctrl+`),然后針對(duì)選定的環(huán)境,選擇“打開(kāi)交互窗口”命令或按鈕。First, switch to the Python Environments window (View > Other Windows > Python Environments or Ctrl+K > Ctrl+`) and select the Open Interactive Window command or button for a chosen environment.
方法二,在“視圖” > “其他窗口”菜單底部附近,可以針對(duì)默認(rèn)環(huán)境使用“Python 交互窗口”命令,另外還有一個(gè)命令可用于切換到環(huán)境窗口:Second, near the bottom of the View > Other Windows menu, there's a Python Interactive Window command for your default environment, as well as a command to switch to the Environments window:
方法三,可以通過(guò)選擇“調(diào)試” > “在 Python 交互窗口中執(zhí)行 ”菜單命令 (Shift+Alt+F5),打開(kāi)項(xiàng)目中啟動(dòng)文件的“交互”窗口,或獨(dú)立文件的“交互”窗口 :Third, you can open an Interactive window on the startup file in your project, or for a stand-alone file, by selecting the Debug > Execute in Python Interactive menu command (Shift+Alt+F5):
最后,可以選中文件中的代碼,然后如下所述,使用發(fā)送到交互命令。Finally, you can select code in file and use the Send to Interactive command described below.
交互窗口選項(xiàng)Interactive window options
可以通過(guò)“工具” > “選項(xiàng)” > “Python” > “交互窗口”控制“交互”窗口的各個(gè)方面(請(qǐng)參閱選項(xiàng)):You can control various aspects of the Interactive window through Tools > Options > Python > Interactive Windows (see Options):
使用交互窗口Use the Interactive window
交互窗口打開(kāi)后,可以在 >>> 提示符處逐行輸入代碼。Once the Interactive window is open, you can start entering code line-by-line at the >>> prompt. 交互窗口會(huì)執(zhí)行輸入的每一行代碼,包括導(dǎo)入模塊、定義變量等:The Interactive window executes each line as you enter it, which includes importing modules, defining variables, and so on:
例外情況是需要其他行的代碼才能補(bǔ)全語(yǔ)句,例如 for 語(yǔ)句以冒號(hào)結(jié)束,如上所示。The exception is when additional lines of code are needed to make a complete statement, such as when a for statement ends in a colon as shown above. 在這些情況下,行提示符將更改為 ...,指示需要輸入程序塊的其他行,如上圖中的第四和第五行所示。In these cases, the line prompt changes to ... indicating that you need to enter additional lines for the block, as shown on the fourth and fifth lines in the graphic above. 在空白行上按 Enter 鍵時(shí),交互窗口會(huì)關(guān)閉程序塊,并在解釋器中運(yùn)行該程序塊。When you press Enter on a blank line, the Interactive window closes the block and runs it in the interpreter.
提示
交互窗口通過(guò)自動(dòng)縮進(jìn)屬于周邊范圍的語(yǔ)句,改進(jìn)常用 Python 命令行的 REPL 體驗(yàn)。The Interactive window improves upon the usual Python command-line REPL experience by automatically indenting statements that belong to a surrounding scope. 其歷史記錄(使用向上鍵重新調(diào)用)還提供多行項(xiàng),而命令行 REPL 僅提供單行。Its history (recalled with the up arrow) also provides multiline items, whereas the command-line REPL provides only single lines.
交互窗口還支持多個(gè)元命令。The Interactive window also supports several meta-commands. 所有元命令都以 $ 開(kāi)頭,你可以鍵入 $help 獲得元命令和 $help 的列表,以獲取特定命令的使用情況詳細(xì)信息。All meta-commands start with $, and you can type $help to get a list of the meta-commands and $help to get usage details for a specific command.
元命令Meta-command
描述Description
$$
插入注釋,用于注釋會(huì)話中的代碼。Inserts a comment, which is helpful to comment code throughout your session.
$attach
將 Visual Studio 調(diào)試器附加到 REPL 窗口進(jìn)程以啟用調(diào)試。Attaches the Visual Studio debugger to the REPL window process to enable debugging.
$cls,$clear$cls, $clear
清除編輯器窗口的內(nèi)容,使歷史記錄和執(zhí)行上下文保持不變。Clears the contents of the editor window, leaving history and execution context intact.
$help
顯示命令列表,或有關(guān)特定命令的幫助。Display a list of commands, or help on a specific command.
$load
從文件加載命令并執(zhí)行,直到完成。Loads commands from file and executes until complete.
$mod
將當(dāng)前范圍切換為指定模塊名稱(chēng)。Switches the current scope to the specified module name.
$reset
將執(zhí)行環(huán)境重置為初始狀態(tài),但保留歷史記錄。Resets the execution environment to the initial state, but keeps history.
$wait
至少等待指定的毫秒數(shù)。Waits for at least the specified number of milliseconds.
Visual Studio 擴(kuò)展還可以通過(guò)實(shí)現(xiàn)和導(dǎo)出 IInteractiveWindowCommand 來(lái)擴(kuò)展命令(示例)。Commands are also extensible by Visual Studio extensions by implementing and exporting IInteractiveWindowCommand (example).
切換范圍Switch scopes
默認(rèn)情況下,項(xiàng)目交互窗口的范圍為項(xiàng)目的啟動(dòng)文件,就像從命令提示符處運(yùn)行一樣。By default, the Interactive window for a project is scoped to the project's startup file as if you ran it from the command prompt. 對(duì)于獨(dú)立文件,其范圍為該文件。For a stand-alone file, it scopes to that file. 但是,在 REPL 會(huì)話期間,可隨時(shí)使用交互窗口頂部的下拉列表菜單更改范圍:At any time during your REPL session, however, the drop-down menu along the top of the Interactive window lets you change scope:
導(dǎo)入模塊后(如鍵入 import importlib),下拉列表中將顯示可切換到該模塊任意范圍的選項(xiàng)。Once you import a module, such as typing import importlib, options appear in the drop-down to switch into any scope in that module. 交互窗口中的消息還會(huì)指示新的范圍,可用于跟蹤會(huì)話期間如何達(dá)到某個(gè)特定狀態(tài)。A message in the Interactive window also indicates the new scope, so you can track how you got to a certain state during your session.
在某個(gè)范圍中輸入 dir() 將顯示該范圍的有效標(biāo)識(shí)符,包括函數(shù)名稱(chēng)、類(lèi)和變量。Entering dir() in a scope displays valid identifiers in that scope, including function names, classes, and variables. 例如,使用 import importlib 后跟 dir() 將顯示以下內(nèi)容:For example, using import importlib followed by dir() shows the following:
發(fā)送到交互命令Send to Interactive command
除了直接在交互窗口中處理代碼,還可以在編輯器中選中代碼,單擊右鍵,并選擇“發(fā)送到交互”,或按 Ctrl+Enter。In addition to working within the Interactive window directly, you can select code in the editor, right-click, and choose Send to Interactive or press Ctrl+Enter.
此命令非常適用于迭代或演化代碼開(kāi)發(fā),包括在開(kāi)發(fā)時(shí)測(cè)試代碼。This command is useful for iterative or evolutionary code development, including testing your code as you develop it. 例如,將一段代碼發(fā)送到交互窗口并顯示其輸出后,可以按向上鍵再次顯示代碼、對(duì)其進(jìn)行修改,并通過(guò)按 Ctrl+Enter 快速測(cè)試。For example, once you've sent a piece of code to the Interactive window and seen its output, you can press the up arrow to show the code again, modify it, and test it quickly by pressing Ctrl+Enter. (在輸入結(jié)束時(shí)按 Enter 將執(zhí)行它,但在輸入過(guò)程中按 Enter 將插入新行。)如果有需要的代碼,可以輕松將其復(fù)制回項(xiàng)目文件。(Pressing Enter at the end of input executes it, but pressing Enter in the middle of input inserts a newline.) Once you have the code you want, you can easily copy it back into your project file.
提示
Visual Studio 默認(rèn)會(huì)刪除 >>> 和 ...將代碼從交互窗口粘貼到編輯器時(shí),REPL 會(huì)發(fā)出提示。By default, Visual Studio removes >>> and ... REPL prompts when pasting code from the Interactive window into the editor. 可以在“工具” > “選項(xiàng)” > “文本編輯器” > “Python” > “高級(jí)”選項(xiàng)卡上使用“粘貼刪除 REPL 提示”選項(xiàng)更改此行為。You can change this behavior on the Tools > Options > Text Editor > Python > Advanced tab using the Paste removes REPL prompts option.
使用代碼單元Work with code cells
代碼單元可用于數(shù)據(jù)分析,并且受到各種文本編輯器支持。Code cells can be used in data analysis and are supported by a variety of text editors.
例如,將代碼文件用作暫存器時(shí),通常有一小部分代碼塊需要一次性全部發(fā)送。For example, when using a code file as a scratchpad, you often have a small block of code you want to send all at once. 為了匯集代碼,可以在單元開(kāi)頭添加以 #%% 開(kāi)頭的注釋,將代碼標(biāo)記為代碼單元,結(jié)束前一個(gè)代碼單元。To group code together, mark the code as a code cell by adding a comment starting with #%% to the beginning of the cell, which ends the previous one. 代碼單元可以折疊和展開(kāi),在代碼單元內(nèi)使用 Ctrl+Enter 會(huì)將整個(gè)單元發(fā)送到交互窗口并移動(dòng)到下一個(gè)代碼單元。Code cells can be collapsed and expanded, and using Ctrl+Enter inside a code cell sends the entire cell to the Interactive window and moves to the next one.
Visual Studio 還會(huì)檢測(cè)以 # In[1]: 等注釋開(kāi)頭的代碼單元,將 Jupyter 筆記本導(dǎo)出為 Python 文件時(shí)會(huì)獲得這種格式。Visual Studio also detects code cells starting with comments like # In[1]:, which is the format you get when exporting a Jupyter notebook as a Python file. 通過(guò)此次檢測(cè),可以輕松地運(yùn)行 Azure Notebooks 的筆記本,只需下載為 Python 文件,在 Visual Studio 中打開(kāi)并使用 Ctrl+Enter 運(yùn)行每個(gè)單元即可。This detection makes it easy to run a notebook from Azure Notebooks by downloading as a Python file, opening in Visual Studio, and using Ctrl+Enter to run each cell.
IntelliSense 的行為IntelliSense behavior
與代碼編輯器中 IntelliSense 僅基于源代碼分析不同,交互窗口中,IntelliSense 基于活動(dòng)的對(duì)象。The Interactive window includes IntelliSense based on the live objects, unlike the code editor in which IntelliSense is based on source code analysis only. 這些建議在交互窗口中更為正確,尤其是在使用動(dòng)態(tài)生成代碼的情況下。These suggestions are more correct in the Interactive window, especially with dynamically generated code. 缺點(diǎn)是具有副作用(如記錄消息)的函數(shù)可能會(huì)影響開(kāi)發(fā)體驗(yàn)。The drawback is that functions with side-effects (such as logging messages) may impact your development experience.
如果此行為造成了困擾,請(qǐng)?jiān)凇巴瓿赡J健苯M的“工具” > “選項(xiàng)” > “Python” > “交互窗口”下更改設(shè)置,如選項(xiàng) - 交互窗口選項(xiàng)所述。If this behavior is a problem, change the settings under Tools > Options > Python > Interactive Windows in the Completion Mode group, as described on Options - Interactive windows options.
總結(jié)
以上是生活随笔為你收集整理的python交互模式切换_Python 交互式窗口 (REPL) - Visual Studio | Microsoft Docs的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: python字符编码使用_python
- 下一篇: 匿名内部类可以访问private_内部类