123 lines
2.5 KiB
Markdown
123 lines
2.5 KiB
Markdown
---
|
|
title: "UV the new python package installer written in Rust"
|
|
date: 2024-02-23
|
|
draft: true
|
|
slug: "uv-the-new-python-package-installer-written-in-rust"
|
|
tags: ["ci/cd", "docker", "python", "tools"]
|
|
type: "programming"
|
|
---
|
|
|
|
## Overview
|
|
|
|
|
|
## UV benchmark
|
|
Let's compare UV and VENV/PIP to see how this new solution performs: according to the creators, UV should be around 80x faster than `python -m venv` and 7x faster than `virtualenv`.
|
|
|
|
After creating virtual environments with these different tools, I was able to obtain the following results:
|
|
|
|
{{< chart >}}
|
|
type: 'bar',
|
|
data: {
|
|
labels: ['uv', 'virtualenv', 'venv'],
|
|
datasets: [{
|
|
data: [0.010, 0.153, 1.539],
|
|
}]
|
|
},
|
|
options: {
|
|
indexAxis: 'y',
|
|
elements: {
|
|
bar: {
|
|
borderWidth: 2,
|
|
}
|
|
},
|
|
responsive: true,
|
|
plugins: {
|
|
legend: {
|
|
display: false,
|
|
},
|
|
title: {
|
|
display: true,
|
|
text: 'Virtual environment creation'
|
|
}
|
|
}
|
|
}
|
|
{{< /chart >}}
|
|
|
|
In my case, UV is ~15x faster than `virtualenv` and ~150x faster than `python -m venv`. I then tried installing python packets with UV and PIP to compare speeds:
|
|
|
|
{{< chart >}}
|
|
type: 'bar',
|
|
data: {
|
|
labels: ['uv', 'uv+cache', 'pip'],
|
|
datasets: [{
|
|
data: [2.961, 0.413, 13.917],
|
|
}]
|
|
},
|
|
options: {
|
|
indexAxis: 'y',
|
|
elements: {
|
|
bar: {
|
|
borderWidth: 2,
|
|
}
|
|
},
|
|
responsive: true,
|
|
plugins: {
|
|
legend: {
|
|
display: false,
|
|
},
|
|
title: {
|
|
display: true,
|
|
text: 'Packages install (~100)'
|
|
}
|
|
}
|
|
}
|
|
{{< /chart >}}
|
|
|
|
For packet insertion I find that UV is ~5x faster than pip and ~33x faster then `pip` with the UV cache.
|
|
|
|
## Docker image for CI/CD
|
|
UV
|
|
|
|
```dockerfile
|
|
FROM python:3.11-slim
|
|
|
|
RUN python3 -m pip install --upgrade pip && \
|
|
pip3 install uv && \
|
|
uv venv && \
|
|
. /.venv/bin/activate && \
|
|
uv pip install ruff
|
|
|
|
ENV VIRTUAL_ENV /.venv
|
|
ENV PATH /.venv/bin:$PATH
|
|
```
|
|
|
|
```bash
|
|
debian@debian:~/dev/images$ docker build -t uv_python .
|
|
[...]
|
|
debian@debian:~/dev/images$ docker run -it --rm uv_python bash
|
|
root@ec910e7ea8eb:/# uv pip install django
|
|
Resolved 3 packages in 483ms
|
|
Downloaded 3 packages in 584ms
|
|
Installed 3 packages in 150ms
|
|
+ asgiref==3.7.2
|
|
+ django==5.0.2
|
|
+ sqlparse==0.4.4
|
|
root@ec910e7ea8eb:/# python3
|
|
Python 3.11.8 (main, Feb 7 2024, 22:49:54) [GCC 12.2.0] on linux
|
|
Type "help", "copyright", "credits" or "license" for more information.
|
|
>>> import django
|
|
>>>
|
|
```
|
|
|
|
```yml
|
|
[...]
|
|
variables:
|
|
UV_CACHE_DIR: "$CI_PROJECT_DIR/.cache/uv"
|
|
|
|
cache:
|
|
- key: uv-pip-cache
|
|
paths:
|
|
- $CI_PROJECT_DIR/.cache/uv
|
|
[...]
|
|
```
|