iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
📝

How to Set Up a Jupyter Notebook Development Environment in VS Code

に公開

Introduction

I have set up a Jupyter Notebook development environment in VSCode, so I am recording the steps here as a memo.
I hope this will be helpful for those who are going to set up a Jupyter Notebook development environment in the future.

Prerequisites

  • VSCode must already be installed.

Requirements

  1. VSCode (Visual Studio Code) - Please install it in advance.
  2. Python - Download and install it.
  3. Jupyter - Install it using pip.

Steps

1. Installing Python

If Python is not installed, please follow the steps below to install it.

Installation

Installing from the official Python website (Windows, macOS)

  • Download and install the installation package from the official Python website. A Python version of 3.6 or later is recommended.
  • Run the downloaded installer to install it.
  • Make sure to check the "Add Python to PATH" checkbox during installation.

Installing Python using Homebrew (macOS)

  • Check if Homebrew is installed
    Open the terminal and run the following command to install Python.

    brew -v
    

    If it is installed, the version will be displayed.

    Homebrew 4.2.15
    
  • If Homebrew is not installed, run the following command to install it

    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
    
  • Install Python

    brew install python
    

Confirmation

  • Verify if Python was installed correctly

    python --version
    

    or

    python3 --version
    

    If installed correctly, the version will be displayed.

    Python 3.12.3
    

3. Installing VSCode Extensions

  • Open VSCode and click the Extensions icon on the left side.
  • Enter "Python" in the search bar and install the Python extension provided by Microsoft.

  • Similarly, enter "Jupyter" and install the Jupyter extension as well.

4. Creating a Virtual Environment

By creating a virtual environment, you can manage different Python versions and packages for each project.

  1. Create a directory (project directory) for the virtual environment.

    If it has already been created, please skip this step.

    mkdir -p ~/dev/project
    
  2. Create the virtual environment.

    python3 -m venv ~/dev/project/venv
    
  3. Activate the virtual environment.

    source ~/dev/project/venv/bin/activate
    

5. Installing Jupyter

Ensure that the virtual environment is activated and install Jupyter.

pip install jupyter

6. Opening Jupyter Notebook in VSCode

  1. Open VSCode

    • Open the project directory in VSCode.
    code ~/dev/project
    

    Alternatively, open VSCode and then open the project directory.

  2. Create a New Jupyter Notebook:

    • Open the Command Palette (Ctrl+Shift+P or Cmd+Shift+P).
    • Select "Jupyter: Create New Jupyter Notebook ".
  3. Select the Kernel:

    • Click "Select Kernel" in the top right of the screen and select the virtual environment you created.
    • If the virtual environment is correctly recognized, venv will be displayed as shown below.

7. Verifying the Environment

  • Enter the following code into a Notebook cell and try running it.

    print("Hello, Jupyter!")
    
  • If it outputs correctly, the environment setup is complete.

Summary

I have introduced the steps for setting up a Jupyter Notebook development environment in VSCode.
By combining VSCode and Jupyter Notebook, you can develop in Python more efficiently.

Discussion