CSFebruary 13, 20256 min read

Jupyter Notebook with uv and VS Code

A step-by-step guide to setting up a Jupyter notebook project using uv for dependency management and VS Code for development.

#Python#Jupyter#uv#VS Code#Data Science

Step 1: Create and Initialize Your Project#

First, you'll create a new project directory and initialize it with uv.

  1. Create a new project folder and move into it:
  1. This command creates a virtual environment in a .venv directory and a pyproject.toml file for managing your project's dependencies.

Step 2: Add ipykernel#

To use a virtual environment as a Jupyter kernel in VS Code, you must have the ipykernel package installed in that environment.

Add ipykernel as a development dependency using the following command:

Step 3: Launch VS Code and Create a Notebook#

Now, you can open your project in VS Code and start a new notebook.

  1. Open the project folder in VS Code from your terminal:
code .
  1. Once VS Code is open, press Ctrl+Shift+P (or Cmd+Shift+P on macOS) to open the Command Palette.
  2. Type "Create: New Jupyter Notebook" and press Enter.

Step 4: Select the Correct Kernel#

When you create the notebook, VS Code will prompt you to select a kernel.

  1. Click on "Select Kernel" in the top-right corner of the notebook interface.

  2. Choose "Python Environments".

  3. Select the virtual environment you created with uv. The path will be similar to:

    • macOS/Linux: .venv/bin/python
    • Windows: .venv\Scripts\python.exe

Your notebook is now connected to your uv-managed environment. You can run code and install packages that will be isolated to this project.

Optional: Manage Dependencies from Within the Notebook#

You can manage your project's dependencies directly from a notebook cell by first adding uv itself as a dependency.

  1. Add uv to your project:
  1. Now, you can use shell commands (!) in a notebook cell to manage packages.

    • To add a package and update pyproject.toml:
  • To install a package without updating project files:

Thanks for reading. If a post helped, share it or keep it for later.

More articles