uv - THE extremely fast Python package installer and resolver !
Posted on Fri 03 January 2025 in python
Did any of you Pythonista folks use any python project without using a virtual-environment, then installing (and maintinaing) versions of your external library :) ?
I do not … and I always found installing (downloading) new libraries slow and cumbersome.
This article will show you how to use uv to make this very very fast and very easy … and the learning curve is inexistant.
How fast it is ?
Let’s test it by using the same requirement.txt
file and disabling using the cache to force download and installating
using uv
It took 0.52 seconds to resolve, download and install the packages listed in the requirements.txt
time uv pip -n install -r requirements.txt
Resolved 23 packages in 168ms
Prepared 23 packages in 305ms
Installed 23 packages in 29ms
+ anyio==4.7.0
+ blinker==1.9.0
+ docutils==0.21.2
+ feedgenerator==2.1.0
+ idna==3.10
+ invoke==2.2.0
+ jinja2==3.1.5
+ livereload==2.7.1
+ markdown==3.7
+ markdown-it-py==3.0.0
+ markupsafe==3.0.2
+ mdurl==0.1.2
+ ordered-set==4.1.0
+ pelican==4.10.2
+ pygments==2.18.0
+ python-dateutil==2.9.0.post0
+ pytz==2024.2
+ rich==13.9.4
+ six==1.17.0
+ sniffio==1.3.1
+ tornado==6.4.2
+ unidecode==1.3.8
+ watchfiles==1.0.3
uv pip -n install -r requirements.txt 0.52s user 0.42s system 144% cpu 0.652 total```
using pip
It took 6.8s when using standerd pip
time pip install --no-cache-dir -r requirements.txt
... more printing content ...
Collecting anyio==4.7.0 (from -r requirements.txt (line 3))
Downloading tornado-6.4.2-cp38-abi3-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (437 kB)
Downloading Unidecode-1.3.8-py3-none-any.whl (235 kB)
Downloading watchfiles-1.0.3-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (442 kB)
Installing collected packages: pytz, unidecode, tornado, sniffio, six, pygments, ordered-set, mdurl, markupsafe, markdown, invoke, idna, feedgenerator, docutils, blinker, python-dateutil, markdown-it-py, livereload, jinja2, anyio, watchfiles, rich, pelican
Successfully installed anyio-4.7.0 blinker-1.9.0 docutils-0.21.2 feedgenerator-2.1.0 idna-3.10 invoke-2.2.0 jinja2-3.1.5 livereload-2.7.1 markdown-3.7 markdown-it-py-3.0.0 markupsafe-3.0.2 mdurl-0.1.2 ordered-set-4.1.0 pelican-4.10.2 pygments-2.18.0 python-dateutil-2.9.0.post0 pytz-2024.2 rich-13.9.4 six-1.17.0 sniffio-1.3.1 tornado-6.4.2 unidecode-1.3.8 watchfiles-1.0.3
pip install --no-cache-dir -r requirements.txt 6.87s user 0.54s system 80% cpu 9.154 total
Using uv we got 13x speed up !
Goal.
Create a python project using uv, add dependencies and test that dependencies are installed.
The toy project will be a static site generated using Pelican which is a static site generator powered by Python!
Install uv.
The compelete installation methis can be found at here
brew install uv
Create a project.
uv init project
cd project
Install Dependencies.
We will install dependecies needed to create, build, generate a site maintained by Pelican.
uv add "pelican[markdown]==4.10.2" "invoke==2.2.0" "livereload==2.7.1"
uv will create automatically:
- Python virtual enviroment under “.venv” folder.
- Pyhton configuration file aka “pyproject.toml”
- Download and install dependencies (and dependencies of dependencies) and update “pyroject.toml” for you.
Test that depencies are properly installed.
uv run python -c "import pelican; print(pelican.__version__)"
>> 4.10.2
Run live reload of the site.
we will use one liner instad of activating a virtual-env then running invoke
# Prefered: one liner option
uv run invoke livereload
# Cumbersome: multi line that works as well :)
source .venv/bin/activate
invoke livereload
Bonus section - the old way (virtual-env and pip) but faster !
In this section we will see how to use uv to generate list of dependencies and installing this the old way. Create a requirements.in
with the direct dependencies that you need.
cat requirements.in
pelican[markdown]==4.10.2
invoke==2.2.0
livereload==2.7.1
# Create a virual environme
uv venv .venv
# Activate virtual environment
source .venv/bin/activate
# compile all the dependencies and dependencies of dependencies ...
uv pip compile requirements.in -o requirements.txt
cat requirements.txt
----
This file was autogenerated by uv via the following command:
# uv pip compile requirements.in -o requirements.txt
anyio==4.7.0
# via watchfiles
blinker==1.9.0
...
----
Installing dependencies
# We will stiff use uv to download and instal libraries.
uv pip install -r requirements.txt