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.
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:
- 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:
Step 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:
-
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
- To install a package without updating project files: