Using Jupyter Notebook in VS Code
VS Code (short for Visual Studio Code) is a free, open-source, cross-platform code editor developed by Microsoft.
You can install extensions in VS Code to use Jupyter Notebook. VS Code's powerful code completion features combined with Jupyter's interactive experience create a perfect match.
If you haven't installed VS Code yet, please visit its official website to download and install it. The installation process is very simpleβjust keep clicking "Next".
If you're not familiar with VS Code, you can refer to our VSCode Tutorial.
Open VS Code, click the Extensions icon in the left activity bar (shortcut Ctrl+Shift+X), search for and install the "Jupyter" extension (provided by Microsoft). Installing this extension will automatically install the Python extension.
Core Features and Operation Guide
Create Your First Jupyter Notebook
Now, let's create a brand new Jupyter Notebook file (.ipynb).
New File: Simply create a new file with the .ipynb extension.
You can also create it through the command palette:
- In VS Code, press
Ctrl+Shift+Pto open the Command Palette (this is a universal search box). - Type
Jupyter: Create New Jupyter Notebookand select it. - VS Code will automatically create a new file named
Untitled-1.ipynband open it in the notebook interface. You'll see the first cell is already ready.
Configure Runtime Environment (Kernel)
Before running code, you need to tell VS Code which Python interpreter to use:
- Click "Select Kernel" in the top-right corner of the editing interface.
- In the popup list, select your installed Python version or Anaconda environment (e.g., Python 3.x.x or base (conda)).
- Note: If this is your first time running, VS Code may prompt you to install the ipykernel packageβjust click "Install".
Then, you can start writing and running code:
Cell Execution and Management
Running Code Cells
- Single cell execution: Click the play icon (βΆ) on the left side of the cell.
- Keyboard shortcuts:
Ctrl + Enter: Run the current cell.Shift + Enter: Run the current cell and jump/select the cell below.Alt + Enter: Run the current cell and insert a new cell below.
- Multiple cell execution: Use the "double arrow" icon Run All in the top toolbar to run all cells; or at a specific cell, select Run Above (run all above) or Run Below (run all below).
- Section-based execution: In the "Outline" view, you can click the button next to a section title to run the entire group of cells under that Markdown heading.
Cell Modes
Cells in VS Code have three states, identified by the vertical bar on the left:
- Unselected: No vertical bar.
- Command Mode: Solid vertical bar, where you can execute keyboard commands (like delete, copy).
Enterenters edit mode;Escreturns to command mode.Ainserts a cell above;Binserts below.D, D(press twice) deletes cell;Zundoes deletion.
- Edit Mode: Solid vertical bar with cell border, where you can input code.
Format Switching
- In Command Mode: Press
Mto switch to Markdown (documentation), pressYto switch to Code.
File Operations and Navigation
- Save:
Ctrl + S. - Export: Click
...> Export in the toolbar. Supports exporting to.pyscript, HTML, or PDF (note: PDF export requires TeX environment installation). - Outline navigation: Quickly jump through the Outline view in the sidebar. By default, only Markdown headings are displayed; you can enable
Notebook > Outline: Show Code Cellsin settings to show code blocks. - Line number control: In command mode, press
Lto toggle line numbers for a single cell, pressShift + Lto toggle line numbers for the entire document.
Data Science Enhancement Tools
Variable Explorer and Data Viewer
Click the Variables icon in the toolbar to open the variable panel at the bottom.
- View details: Double-click a variable or click the icon next to it to enter the Data Viewer.
- Filter data: Enter content in the text box at the top of a column to search; enter
=for exact match; supports regex filtering.
Plot Saving
Hover over a generated chart (such as matplotlib output), click the Save icon in the top-right corner to save it as an image.
Debugging and Advanced Features
Debugging Features
- Run by Line: Click the icon in the cell toolbar to step through code execution without interruption.
- Full debugging: Set a breakpoint on the left side of a cell, select Debug Cell next to the
Runbutton.
Remote Connection
If you need to use computing power from a remote server:
- Click the Kernel Picker in the top-right corner.
- Select Existing Jupyter Server.
- Enter the server URL with
?token=to connect.
Diff Comparison
Since .ipynb is essentially JSON, VS Code provides a visual comparison tool. You can clearly see specific changes in inputs, outputs, or metadata without having to read messy raw code.
YouTip