ZhangZhihui's Blog  

 

 

 

pip install jupyter

 

 

Python Interactive window

Jupyter (formerly IPython Notebook) is an open-source project that lets you easily combine Markdown text and executable Python source code on one canvas called a notebook. Visual Studio Code supports working with Jupyter Notebooks natively, as well as through Python code files. This topic covers the support offered through Python code files and demonstrates how to:

  • Work with Jupyter-like code cells
  • Run code in the Python Interactive Window
  • View, inspect, and filter variables using the Variables Explorer and Data Viewer
  • Connect to a remote Jupyter server
  • Debug a Jupyter notebook
  • Export a Jupyter notebook

To work with Jupyter notebooks, you must activate an Anaconda environment in VS Code, or another Python environment in which you've installed the Jupyter package. To select an environment, use the Python: Select Interpreter command from the Command Palette (Ctrl+Shift+P).

Once the appropriate environment is activated, you can create and run Jupyter-like code cells, connect to a remote Jupyter server for running code cells, and export Python files as Jupyter notebooks.

Jupyter code cells

You define Jupyter-like code cells within Python code using a # %% comment:

# %%
msg = "Hello World"
print(msg)

# %%
msg = "Hello again"
print(msg)

Note: Make sure to save the code shown above in a file with a .py extension.

When the Python extension detects a code cell, it adds Run Cell and Debug Cell CodeLens adornments. The first cell also includes Run Below and all subsequent cells include Run Above:

Jupyter adornments for code cells in the VS Code editor

Note: By default, Debug Cell just steps into user code. If you want to step into non-user code, you need to uncheck Debug Just My Code in the Jupyter extension settings (Ctrl+,).

Run Cell applies to only the one code cell. Run Below, which appears on the first cell, runs all the code in the file. Run Above applies to all the code cells up to, but not including, the cell with the adornment. You would use Run Above, for example, to initialize the state of the runtime environment before running that specific cell.

Selecting a command starts Jupyter (if necessary, which might take a minute), then runs the appropriate cell(s) in the Python Interactive window:

Code cells running in a Python Interactive window

You can also run code cells using (Ctrl+Enter) or the Python: Run Selection/Line in Python Terminal command (Shift+Enter). After using this command, the Python extension automatically moves the cursor to the next cell. If you're in the last cell in the file, the extension automatically inserts another # %% delimiter for a new cell, mimicking the behavior of a Jupyter notebook.

You can also click in the margin to the left of line numbers to set breakpoints. Then you can use Debug Cell to start a debugging session for that code cell. The debugger stops execution at breakpoints and allows you to step through code one line at a time and inspect variables (see Debugging for details).

Additional commands and keyboard shortcuts

The following table lists additional commands and keyboard shortcuts supported when working with code cells.

CommandKeyboard shortcut
Python: Go to Next Cell Ctrl+Alt+]
Python: Go to Previous Cell Ctrl+Alt+[
Python: Extend Selection by Cell Above Ctrl+Shift+Alt+[
Python: Extend Selection by Cell Below Ctrl+Shift+Alt+]
Python: Move Selected Cells Up Ctrl+; U
Python: Move Selected Cells Down Ctrl+; D
Python: Insert Cell Above Ctrl+; A
Python: Insert Cell Below Ctrl+; B
Python: Insert Cell Below Position Ctrl+; S
Python: Delete Selected Cells Ctrl+; X
Python: Change Cell to Code Ctrl+; C
Python: Change Cell to Markdown Ctrl+; M

Using the Python Interactive window

The Python Interactive window, mentioned in the previous section, can be used as a standalone console with arbitrary code (with or without code cells). To use the window as a console, open it with the Jupyter: Create Interactive Window command from the Command Palette. You can then type in code, using Enter to go to a new line and Shift+Enter to run the code.

To use the window with a file, use the Jupyter: Run Current File in Python Interactive Window command from the Command Palette.

 

For more, see https://code.visualstudio.com/docs/python/jupyter-support-py .

 

posted on 2025-04-20 10:38  ZhangZhihuiAAA  阅读(26)  评论(0)    收藏  举报