Jupyter Notebook with uv and VS Code
- Python
- Jupyter
- uv
- VS Code
Learn how to set up a Jupyter notebook project with uv for fast, reliable dependency management and VS Code for a great development experience.
Step 1: Create and Initialize Your Project
First, you'll create a new project directory and initialize it with uv.
- Create a new project folder and move into it:
uv init my-notebook-project
cd my-notebook-project- This command creates a virtual environment in a
.venvdirectory and apyproject.tomlfile 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:
uv add --dev ipykernelStep 3: Launch VS Code and Create a Notebook
Now, you can open your project in VS Code and start a new notebook.
- Open the project folder in VS Code from your terminal:
code .- Once VS Code is open, press
Ctrl+Shift+P(orCmd+Shift+Pon macOS) to open the Command Palette. - 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.
-
Click on "Select Kernel" in the top-right corner of the notebook interface.
-
Choose "Python Environments".
-
Select the virtual environment you created with
uv. The path will be similar to:- macOS/Linux:
.venv/bin/python - Windows:
.venv\Scripts\python.exe
- macOS/Linux:
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.
- Add
uvto your project:
uv add --dev uv-
Now, you can use shell commands (
!) in a notebook cell to manage packages.- To add a package and update
pyproject.toml:
- To add a package and update
!uv add pydantic- To install a package without updating project files:
!uv pip install pandas