Friday, November 14, 2025

Creating a kernel in a UV project for jupyter notebooks in VS Code


This guide explains how to set up a dedicated Jupyter kernel for your uv project that uses the project's own virtual environment (.venv).



1. Install ipykernel as a Development Dependency

First, ensure that your project has ipykernel installed so that it can create kernels for Jupyter.

uv add --dev ipykernel 
  • --dev: installs it as a development dependency.

  • ipykernel: the package that allows creating Jupyter kernels.


2. Create a Jupyter Kernel for Your Project's .venv

Run the following command to register a Jupyter kernel that points to the project's virtual environment:

uv run ipython kernel install --user --env VIRTUAL_ENV=$(pwd)/.venv --name=project 

Explanation of the command:

  • uv run ipython kernel install: runs the IPython kernel installation inside the uv environment.

  • --user: installs the kernel only for the current user.

  • --env VIRTUAL_ENV=$(pwd)/.venv: points the kernel to use your project's virtual environment located at .venv.

  • --name=project: the name of the kernel that will appear in Jupyter.


3. Reload VS Code (if using VS Code)

After creating the kernel, reload your VS Code window to make it available:

  1. Press Ctrl+Shift+P (or Cmd+Shift+P on Mac) to open the Command Palette.

  2. Type Reload Window and hit Enter.


4. Select the Kernel in Jupyter Notebook or VS Code

  1. Open your Jupyter notebook.

  2. Click on the kernel name (top right corner) or Select Kernel.

  3. Choose the newly created kernel project.


Notes

  • Make sure your .venv is already created and has Python installed.

  • Using this method ensures that the notebook runs in the same environment as your uv project.

  • This is especially useful when working with isolated dependencies for different projects.

No comments:

Post a Comment

Generator Expression vs List Comprehension in Python

 When handling large datasets in Python, both performance and memory usage are key concerns. Python offers two powerful tools for creating s...