Using Jupyter with uv

I think the package manager uv (by astral) simplifies working with jupyter a lot, and therefore I’d like to share here some snippets that I find useful.
uv can be installed via: Installation | uv

Installation-and-first-run, as well as all future runs, is the same command:
uvx jupyter lab

Choosing a python version:
uvx --python 3.10 jupyter lab
as well as:
uvx --python 3.13 jupyter lab

using a python package (e.g. anywidget):
uvx --python 3.12 --with anywidget jupyter lab

and finally, installing packages within a notebook cell
!uv pip install matplotlib

It’s worth mentioning that the above snippets are great for prototyping ideas or running one-off scripts that don’t live within a project.

I’ve also made a twitter thread about this with some screen recording benchmarks:

Feel free to share other ideas about using Jupyter with uv in this thread!

2 Likes

Thanks for sharing! Can you elaborate on why this is easier/simpler/better? I have a decent guess on which parts of the experience it improves but it would be much easier to link to this post if it included a problem statement as some background context on status quo :slight_smile:

1 Like

Thanks for your interest @krassowski!
I’ll elaborate a bit more on this.

Firstly, it’s faster.
On macOS, this is the status quo on how I would install Jupyter with brew:

1. install brew
2. install python
3. make a virtual environment
4. activate environment
5. install jupyter
6. start jupyter lab

It takes about 5 minutes for the steps 2-6 (video documentation here)
With UV, this simplifies to

1. install uv
2. uvx jupyter lab

this takes about 30 seconds (video documentation here)

It’s faster because you have to memorize and write fewer lines of code, but also uv itself is 10-100x faster than pip (according to their docs uv).

Then, It’s also more flexible. In the status quo, I’d have to run steps 2-6 again if I wanted to test different python versions, that includes creating and deleting virtual environments every time. With uv on the other hand, it automatically fetches the right python version, e.g. by using

uvx --python 3.12 jupyter lab
uvx --python 3.13 jupyter lab

Another thought about the flexibility: You can run notebooks without activating virtual environments. uv internally caches dependencies, and then quickly rebuilds the one-time environment on the fly (as far as I understand).

As a final note, I want to mention that marimo notebooks (not jupyter notebooks though) can include 3rd-party dependencies within the script, based on PEP 722 – Dependency specification for single-file scripts | peps.python.org
That means that I can e.g. run this gist
with just a single command uvx marimo edit --sandbox color_picker.py and it will fully reproduce my notebook, including all dependencies. Would be amazing if that would work for jupyter notebooks as well!

3 Likes