日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python交互模式切换_Python 交互式窗口 (REPL) - Visual Studio | Microsoft Docs

發布時間:2024/8/1 python 57 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python交互模式切换_Python 交互式窗口 (REPL) - Visual Studio | Microsoft Docs 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

使用 Python 交互窗口Work with the Python Interactive window

02/11/2019

本文內容

Visual Studio 為每個 Python 環境提供交互讀取-評估-打印-循環 (REPL) 窗口,改進了在命令行中運行 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. 借助交互窗口(通過“視圖” > “其他窗口” > “<環境>交互”菜單命令打開),可以輸入任意 Python 代碼,并查看即時結果。The Interactive window (opened with the View > Other Windows > Interactive menu commands) lets you enter arbitrary Python code and see immediate results. 這種編碼方式有助于了解與實驗 API 和庫,并以交互方式開發要包含在項目中的工作代碼。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

調試Debugging

映像Images

標準Standard

默認 REPL,直接與 Python 通信Default REPL, talks to Python directly

標準編輯(多行等)。Standard editing (multiline, etc.).

是,通過 $attachYes, via $attach

否No

調試Debug

默認 REPL,與已調試的 Python 進程通信Default REPL, talks to debugged Python process

標準編輯Standard editing

僅調試Only debugging

否No

IPythonIPython

REPL 與 IPython 后端通信REPL talks to IPython backend

IPython 命令,Pylab 的便利IPython commands, Pylab conveniences

否No

是,在 REPL 中內聯Yes, inline in REPL

帶 Pylab 的 IPythonIPython w/o Pylab

REPL 與 IPython 后端通信REPL talks to IPython backend

標準 IPythonStandard IPython

否No

是,單獨窗口Yes, separate window

本文介紹標準 REPL 模式和調試 REPL 模式。This article describes the Standard and Debug REPL modes. 有關 IPython 模式的詳細信息,請參閱使用 IPython REPL。For details on IPython modes, see Use the IPython REPL.

有關包含示例的詳細演練,包括與編輯器的交互(如 Ctrl+Enter),請參閱教程步驟 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.

打開交互窗口Open an Interactive window

以下有幾種方法可用于針對某個環境打開交互窗口。There are several ways to open the Interactive window for an environment.

方法一,切換到 Python 環境窗口(“視圖” > “其他窗口” > “Python 環境”或 Ctrl+K > Ctrl+`),然后針對選定的環境,選擇“打開交互窗口”命令或按鈕。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.

方法二,在“視圖” > “其他窗口”菜單底部附近,可以針對默認環境使用“Python 交互窗口”命令,另外還有一個命令可用于切換到環境窗口: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:

方法三,可以通過選擇“調試” > “在 Python 交互窗口中執行 ”菜單命令 (Shift+Alt+F5),打開項目中啟動文件的“交互”窗口,或獨立文件的“交互”窗口 :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):

最后,可以選中文件中的代碼,然后如下所述,使用發送到交互命令。Finally, you can select code in file and use the Send to Interactive command described below.

交互窗口選項Interactive window options

可以通過“工具” > “選項” > “Python” > “交互窗口”控制“交互”窗口的各個方面(請參閱選項):You can control various aspects of the Interactive window through Tools > Options > Python > Interactive Windows (see Options):

使用交互窗口Use the Interactive window

交互窗口打開后,可以在 >>> 提示符處逐行輸入代碼。Once the Interactive window is open, you can start entering code line-by-line at the >>> prompt. 交互窗口會執行輸入的每一行代碼,包括導入模塊、定義變量等:The Interactive window executes each line as you enter it, which includes importing modules, defining variables, and so on:

例外情況是需要其他行的代碼才能補全語句,例如 for 語句以冒號結束,如上所示。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 鍵時,交互窗口會關閉程序塊,并在解釋器中運行該程序塊。When you press Enter on a blank line, the Interactive window closes the block and runs it in the interpreter.

提示

交互窗口通過自動縮進屬于周邊范圍的語句,改進常用 Python 命令行的 REPL 體驗。The Interactive window improves upon the usual Python command-line REPL experience by automatically indenting statements that belong to a surrounding scope. 其歷史記錄(使用向上鍵重新調用)還提供多行項,而命令行 REPL 僅提供單行。Its history (recalled with the up arrow) also provides multiline items, whereas the command-line REPL provides only single lines.

交互窗口還支持多個元命令。The Interactive window also supports several meta-commands. 所有元命令都以 $ 開頭,你可以鍵入 $help 獲得元命令和 $help 的列表,以獲取特定命令的使用情況詳細信息。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

$$

插入注釋,用于注釋會話中的代碼。Inserts a comment, which is helpful to comment code throughout your session.

$attach

將 Visual Studio 調試器附加到 REPL 窗口進程以啟用調試。Attaches the Visual Studio debugger to the REPL window process to enable debugging.

$cls,$clear$cls, $clear

清除編輯器窗口的內容,使歷史記錄和執行上下文保持不變。Clears the contents of the editor window, leaving history and execution context intact.

$help

顯示命令列表,或有關特定命令的幫助。Display a list of commands, or help on a specific command.

$load

從文件加載命令并執行,直到完成。Loads commands from file and executes until complete.

$mod

將當前范圍切換為指定模塊名稱。Switches the current scope to the specified module name.

$reset

將執行環境重置為初始狀態,但保留歷史記錄。Resets the execution environment to the initial state, but keeps history.

$wait

至少等待指定的毫秒數。Waits for at least the specified number of milliseconds.

Visual Studio 擴展還可以通過實現和導出 IInteractiveWindowCommand 來擴展命令(示例)。Commands are also extensible by Visual Studio extensions by implementing and exporting IInteractiveWindowCommand (example).

切換范圍Switch scopes

默認情況下,項目交互窗口的范圍為項目的啟動文件,就像從命令提示符處運行一樣。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. 對于獨立文件,其范圍為該文件。For a stand-alone file, it scopes to that file. 但是,在 REPL 會話期間,可隨時使用交互窗口頂部的下拉列表菜單更改范圍:At any time during your REPL session, however, the drop-down menu along the top of the Interactive window lets you change scope:

導入模塊后(如鍵入 import importlib),下拉列表中將顯示可切換到該模塊任意范圍的選項。Once you import a module, such as typing import importlib, options appear in the drop-down to switch into any scope in that module. 交互窗口中的消息還會指示新的范圍,可用于跟蹤會話期間如何達到某個特定狀態。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.

在某個范圍中輸入 dir() 將顯示該范圍的有效標識符,包括函數名稱、類和變量。Entering dir() in a scope displays valid identifiers in that scope, including function names, classes, and variables. 例如,使用 import importlib 后跟 dir() 將顯示以下內容:For example, using import importlib followed by dir() shows the following:

發送到交互命令Send to Interactive command

除了直接在交互窗口中處理代碼,還可以在編輯器中選中代碼,單擊右鍵,并選擇“發送到交互”,或按 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.

此命令非常適用于迭代或演化代碼開發,包括在開發時測試代碼。This command is useful for iterative or evolutionary code development, including testing your code as you develop it. 例如,將一段代碼發送到交互窗口并顯示其輸出后,可以按向上鍵再次顯示代碼、對其進行修改,并通過按 Ctrl+Enter 快速測試。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. (在輸入結束時按 Enter 將執行它,但在輸入過程中按 Enter 將插入新行。)如果有需要的代碼,可以輕松將其復制回項目文件。(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 默認會刪除 >>> 和 ...將代碼從交互窗口粘貼到編輯器時,REPL 會發出提示。By default, Visual Studio removes >>> and ... REPL prompts when pasting code from the Interactive window into the editor. 可以在“工具” > “選項” > “文本編輯器” > “Python” > “高級”選項卡上使用“粘貼刪除 REPL 提示”選項更改此行為。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

代碼單元可用于數據分析,并且受到各種文本編輯器支持。Code cells can be used in data analysis and are supported by a variety of text editors.

例如,將代碼文件用作暫存器時,通常有一小部分代碼塊需要一次性全部發送。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. 為了匯集代碼,可以在單元開頭添加以 #%% 開頭的注釋,將代碼標記為代碼單元,結束前一個代碼單元。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. 代碼單元可以折疊和展開,在代碼單元內使用 Ctrl+Enter 會將整個單元發送到交互窗口并移動到下一個代碼單元。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 還會檢測以 # In[1]: 等注釋開頭的代碼單元,將 Jupyter 筆記本導出為 Python 文件時會獲得這種格式。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. 通過此次檢測,可以輕松地運行 Azure Notebooks 的筆記本,只需下載為 Python 文件,在 Visual Studio 中打開并使用 Ctrl+Enter 運行每個單元即可。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 基于活動的對象。The Interactive window includes IntelliSense based on the live objects, unlike the code editor in which IntelliSense is based on source code analysis only. 這些建議在交互窗口中更為正確,尤其是在使用動態生成代碼的情況下。These suggestions are more correct in the Interactive window, especially with dynamically generated code. 缺點是具有副作用(如記錄消息)的函數可能會影響開發體驗。The drawback is that functions with side-effects (such as logging messages) may impact your development experience.

如果此行為造成了困擾,請在“完成模式”組的“工具” > “選項” > “Python” > “交互窗口”下更改設置,如選項 - 交互窗口選項所述。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.

總結

以上是生活随笔為你收集整理的python交互模式切换_Python 交互式窗口 (REPL) - Visual Studio | Microsoft Docs的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。