Skip to content

Repo sync for protected CLA branch #9480

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions docs/debugger/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
href: getting-started-with-the-debugger-cpp.md
- name: Learn to debug Visual Basic
href: ../get-started/visual-basic/tutorial-debugger.md?toc=%2fvisualstudio%2fdebugger%2ftoc.json
- name: Running Python code in the debugger...
- name: Running Python code in the debugger >>
href: ../python/tutorial-working-with-python-in-visual-studio-step-04-debugging.md
- name: Debug a live Azure app
href: debug-live-azure-applications.md
Expand Down Expand Up @@ -424,13 +424,13 @@
href: ../xaml-tools/debug-xaml-in-blend.md
- name: How to trigger suspend, resume, and background events
href: how-to-trigger-suspend-resume-and-background-events-for-windows-store-apps-in-visual-studio.md
- name: Managed code (C#, Visual Basic, F#)
- name: .NET
items:
- name: Debugging preparation
items:
- name: 'Debugging preparation: C#, F#, and Visual Basic project types'
- name: 'Debugging preparation: .NET'
href: debugging-preparation-csharp-f-hash-and-visual-basic-project-types.md
- name: 'Managed debugging: Recommended property settings'
- name: Recommended property settings
href: managed-debugging-recommended-property-settings.md
- name: 'Debugging preparation: Windows Forms applications'
href: debugging-preparation-windows-forms-applications.md
Expand Down Expand Up @@ -492,10 +492,8 @@
href: mfc-debugging-techniques.md
- name: CRT debugging techniques >>
href: /cpp/c-runtime-library/crt-debugging-techniques
- name: Native code debugging FAQs
items:
- name: Debugging native code FAQs
href: debugging-native-code-faqs.yml
- name: FAQ for debugging native code
href: debugging-native-code-faqs.yml
- name: COM
items:
- name: COM server and container debugging
Expand Down
106 changes: 54 additions & 52 deletions docs/python/debugging-python-in-visual-studio.md

Large diffs are not rendered by default.

Binary file modified docs/python/media/debugging-breakpoints.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/python/media/debugging-exception-popup.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/python/media/debugging-exception-settings.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/python/media/debugging-interactive.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/python/media/debugging-locals-window.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/python/media/debugging-quick-tips.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/python/media/debugging-tracepoint.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/python/media/debugging-watch-window.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/python/media/environments/Environments-Project.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/python/media/environments/environments-project-2019.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/python/media/interactive-importlib-scope.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/python/media/interactive-scopes.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/python/media/interactive-send-to.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/python/media/interactive-window-opening.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/python/media/interactive-window.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/python/media/options-interactive-windows.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
63 changes: 28 additions & 35 deletions docs/python/python-interactive-repl-in-visual-studio.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: Python interactive window (REPL)
description: Use the interactive window (REPL) for rapid Python code development in Visual Studio.
ms.date: 02/06/2022
ms.date: 07/28/2023
ms.topic: how-to
author: cwebster-99
ms.author: cowebster
Expand All @@ -12,22 +12,23 @@ ms.workload:
- python
- data-science
---

# Work with the Python Interactive window

[!INCLUDE [Visual Studio](~/includes/applies-to-version/vs-windows-only.md)]
[!INCLUDE [Visual Studio](~/includes/applies-to-version/vs-windows-only.md)]

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. The **Interactive** window (opened with the **View** > **Other Windows** > **<environment> Interactive** menu commands) lets you enter arbitrary Python code and see immediate results. This way of coding helps you learn and experiment with APIs and libraries and interactively develop working code to include in your projects.
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. The **Interactive** window (opened with the **View** > **Other Windows** > **<environment> Interactive** menu commands) lets you enter arbitrary Python code and see immediate results. This way of coding helps you learn and experiment with APIs and libraries and interactively develop working code to include in your projects.

:::image type="content" source="media/interactive-window.png" alt-text="Screenshot showing the Python interactive read-evaluate-print loop (REPL) window.":::

Visual Studio has many Python REPL modes to choose from:

| REPL | Description | Editing | Debugging | Images |
| --- | --- | --- | --- | --- |
| Standard | Default REPL talks to Python directly | Standard editing (multiline, and so on). | Yes, via `$attach` | No |
| Debug | Default REPL talks to debugged Python process | Standard editing | Only debugging | No |
| IPython | REPL talks to IPython backend | IPython commands, Pylab conveniences | No | Yes, inline in REPL |
| IPython w/o Pylab | REPL talks to IPython backend | Standard IPython | No | Yes, separate window |
| REPL | Description | Editing | Debugging | Images |
| ----------------- | --------------------------------------------- | ---------------------------------------- | ------------------ | -------------------- |
| Standard | Default REPL talks to Python directly | Standard editing (multiline, and so on). | Yes, via `$attach` | No |
| Debug | Default REPL talks to debugged Python process | Standard editing | Only debugging | No |
| IPython | REPL talks to IPython backend | IPython commands, Pylab conveniences | No | Yes, inline in REPL |
| IPython w/o Pylab | REPL talks to IPython backend | Standard IPython | No | Yes, separate window |

This article describes the **Standard** and **Debug** REPL modes. For details on IPython modes, see [Use the IPython REPL](interactive-repl-ipython.md).

Expand Down Expand Up @@ -65,20 +66,20 @@ Once the **Interactive** window is open, you can start entering code line-by-lin

The exception is when more 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 more lines for the block, as shown on the fourth and fifth lines in the graphic above. When you press **Enter** on a blank line, the **Interactive** window closes the block and runs it in the interpreter.

> [!Tip]
> The **Interactive** window improves upon the usual Python command-line REPL experience by automatically indenting statements that belong to a surrounding scope. Its history (recalled with the up arrow) also provides multiline items, whereas the command-line REPL provides only single lines.
<a name="meta-commands"></a>
The **Interactive** window also supports several meta-commands. All meta-commands start with `$`, and you can type `$help` to get a list of the meta-commands and `$help <command>` to get usage details for a specific command.
> [!Tip]
> The **Interactive** window improves upon the usual Python command-line REPL experience by automatically indenting statements that belong to a surrounding scope. Its history (recalled with the up arrow) also provides multiline items, whereas the command-line REPL provides only single lines.
> <a name="meta-commands"></a>
> The **Interactive** window also supports several meta-commands. All meta-commands start with `$`, and you can type `$help` to get a list of the meta-commands and `$help <command>` to get usage details for a specific command.

| Meta-command | Description |
| --- | --- |
| `$$` | Inserts a comment, which is helpful to comment code throughout your session. |
| Meta-command | Description |
| ---------------- | --------------------------------------------------------------------------------------- |
| `$$` | Inserts a comment, which is helpful to comment code throughout your session. |
| `$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. |
| `$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. |

Commands are also extensible by Visual Studio extensions by implementing and exporting `IInteractiveWindowCommand` ([example](https://github.com/Microsoft/PTVS/blob/master/Python/Product/PythonTools/PythonTools/Repl/InteractiveWindowCommands.cs#L85)).

Expand All @@ -103,10 +104,11 @@ In addition to working within the **Interactive** window directly, you can selec

This command is useful for iterative or evolutionary code development, including testing your code as you develop it. 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**. (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.

> [!Tip]
> By default, Visual Studio removes **>>>** and **...** REPL prompts when pasting code from the **Interactive** window into the editor. You can change this behavior on the **Tools** > **Options** > **Text Editor** > **Python** > **Advanced** tab using the **Paste removes REPL prompts** option. See [Options - Miscellaneous options](python-support-options-and-settings-in-visual-studio.md#miscellaneous-options).
> [!Tip]
> By default, Visual Studio removes **>>>** and **...** REPL prompts when pasting code from the **Interactive** window into the editor. You can change this behavior on the **Tools** > **Options** > **Text Editor** > **Python** > **Advanced** tab using the **Paste removes REPL prompts** option. See [Options - Miscellaneous options](python-support-options-and-settings-in-visual-studio.md#miscellaneous-options).

<!-- After 15.3 is released, you can also press **Undo** after pasting to restore prompts. Press **Undo** a second time to remove the pasted code entirely. -->

:::moniker-end

:::moniker range=">=vs-2022"
Expand All @@ -116,21 +118,12 @@ In addition to working within the **Interactive** window directly, you can selec

This command is useful for iterative or evolutionary code development, including testing your code as you develop it. 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**+**E**. (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.

> [!Tip]
> By default, Visual Studio removes **>>>** and **...** REPL prompts when pasting code from the **Interactive** window into the editor. You can change this behavior on the **Tools** > **Options** > **Text Editor** > **Python** > **Advanced** tab using the **Paste removes REPL prompts** option. See [Options - Miscellaneous options](python-support-options-and-settings-in-visual-studio.md#miscellaneous-options).
> [!Tip]
> By default, Visual Studio removes **>>>** and **...** REPL prompts when pasting code from the **Interactive** window into the editor. You can change this behavior on the **Tools** > **Options** > **Text Editor** > **Python** > **Advanced** tab using the **Paste removes REPL prompts** option. See [Options - Miscellaneous options](python-support-options-and-settings-in-visual-studio.md#miscellaneous-options).

<!-- After 15.3 is released, you can also press **Undo** after pasting to restore prompts. Press **Undo** a second time to remove the pasted code entirely. -->
:::moniker-end

## Work with code cells

Code cells can be used in data analysis and are supported by various 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. 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 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. This detection makes it easy to run a notebook from [Azure Notebooks](https://notebooks.azure.com/) by downloading it as a Python file, opening in Visual Studio, and using **Ctrl**+**Enter** to run each cell.

:::image type="content" source="media/interactive-code-cells.png" alt-text="Screenshot showing Interactive code cells.":::
:::moniker-end

## IntelliSense behavior

Expand Down
Loading