DEV Community

Konstantinos Blatsoukas
Konstantinos Blatsoukas

Posted on • Edited on

another uv post :)

installing uv

For installing uv, only curl tool required.

curl -LsSf https://astral.sh/uv/install.sh | sh
Enter fullscreen mode Exit fullscreen mode

uv site

starting a project

uv init example

## if you want to specify a python version, you can that, using --python option
uv init example --python 3.11
Enter fullscreen mode Exit fullscreen mode

this produces a project under a folder name example

example/
├── .gitignore
├── .python-version
├── README.md
├── hello.py
└── pyproject.toml
Enter fullscreen mode Exit fullscreen mode

In the toml file the the required python version is specified (also in the .python-version file).
Also, in the same file the project dependencies are specified, as well.

running a project

uv run hello.py
# program output
Using CPython 3.11.11
Creating virtual environment at: .venv
Hello from example!
Enter fullscreen mode Exit fullscreen mode

Python 3.11.11 wasn't installed on my local system.
uv downloaded the specified version and also created a virtual environment for us.

Let's examine the folder structure, after the

uv run

command.

.
├── .python-version # default python version
├── .venv
├── README.md
├── hello.py
├── pyproject.toml
└── uv.lock
Enter fullscreen mode Exit fullscreen mode

A .venv was created for us, automatically! and uv.lock file (which includes the dependency details, more bellow)

managing dependencies

You can add/remove projects dependencies in pyproject.toml file, on dependencies list:

[project]
name = "backend"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.12"
dependencies = [
    "aio-pika==9.5.0",
    "aiormq==6.8.1",
]
Enter fullscreen mode Exit fullscreen mode

In the above example, we added two dependencies aio-pika and aiormq.

After the toml editing, a sync is required (sync updates also the lock file, venv etc.)

uv sync
Enter fullscreen mode Exit fullscreen mode

The other way is to use the following commands:

uv add 'aio-pika==9.5.0'
uv add 'aiormq==6.8.1'
Enter fullscreen mode Exit fullscreen mode

(Imagine that the dependencies were empty) The above commands will add both libs in the dependency list (toml file), update the venv, and the lock file.
Moreover, uv will download and install all the required transitive dependencies.

More info about maanging dependencies, you can find here:

uv managing dependencies

deps using pip vs uv

This is a simple test for comparing how much time pip install takes to install libs vs the uv.

Imagine that our current project needs the following libs, to be installed:

aio_pika
asyncpg
boto3
datadog
fastapi
geojson
httpx
loguru
pydantic_settings
pypika
python-jose
python-multipart
uvicorn
Enter fullscreen mode Exit fullscreen mode

Let's try doing this using pip and using uv.

using pip

python -m venv vevn
source vevn/bin/activate
pip install pip-tools
pip-compile requirements.in
pip install --no-cache-dir -r requirements.txt
Enter fullscreen mode Exit fullscreen mode

using time in front of the

pip install

I got the following time durations:

real    0m16.852s
user    0m9.211s
sys     0m0.811s
Enter fullscreen mode Exit fullscreen mode

using uv

All the above libs are included on toml file dependencies list:

uv sync --no-cache
Enter fullscreen mode Exit fullscreen mode

using time in front of the

uv syn --no-cache

, I got the following time durations:

real    0m2.553s
user    0m1.511s
sys     0m1.399s
Enter fullscreen mode Exit fullscreen mode

comparing the real time, we get a X6.5 reduction time!

changing python version for a project

uv python pin 3.13 # no need for python 3.13 to be present (if not, it will be installed automatically)
Updated `.python-version` from `3.11` -> `3.13`
Enter fullscreen mode Exit fullscreen mode

let's try now to run again our project

Using CPython 3.13.1
Removed virtual environment at: .venv
Creating virtual environment at: .venv
Hello from example!
Enter fullscreen mode Exit fullscreen mode

CPython 3.13.1, downloaded and installed.
Moreover, previous vevnv automatically deleted and a new one was created.

not sure if I want to migrate to uv

If you are not sure of using uv, you can get some of the benefits, by just putting


 word in front of any pip command that you have:



```sh
# use this:
uv pip compile requirements.in -o requirements.txt
# instead of this
# pip-compile requirements.in -o requirements.txt
Enter fullscreen mode Exit fullscreen mode

You will get the dependencies much faster.

python management

installing the latest version

uv install python
Enter fullscreen mode Exit fullscreen mode

installing specific version

uv install python 3.12
Enter fullscreen mode Exit fullscreen mode

installing multiple python versions

uv install python 3.11 3.12 
Enter fullscreen mode Exit fullscreen mode

viewing python versions

uv python list

#output
cpython-3.14.0a4+freethreaded-linux-x86_64-gnu    <download available>
cpython-3.14.0a4-linux-x86_64-gnu                 <download available>
cpython-3.13.1+freethreaded-linux-x86_64-gnu      <download available>
cpython-3.13.1-linux-x86_64-gnu                   <download available>
cpython-3.12.8-linux-x86_64-gnu                   /home/kostas/.local/share/uv/python/cpython-3.12.8-linux-x86_64-gnu/bin/python3.12
cpython-3.11.11-linux-x86_64-gnu                  /home/kostas/.local/share/uv/python/cpython-3.11.11-linux-x86_64-gnu/bin/python3.11
cpython-3.10.16-linux-x86_64-gnu                  <download available>
cpython-3.10.12-linux-x86_64-gnu                  /usr/bin/python3.10
cpython-3.10.12-linux-x86_64-gnu                  /usr/bin/python3 -> python3.10
cpython-3.10.12-linux-x86_64-gnu                  /usr/bin/python -> /usr/bin/python3
cpython-3.10.12-linux-x86_64-gnu                  /bin/python3.10
cpython-3.10.12-linux-x86_64-gnu                  /bin/python3 -> python3.10
cpython-3.10.12-linux-x86_64-gnu                  /bin/python -> /usr/bin/python3
cpython-3.9.21-linux-x86_64-gnu                   <download available>
cpython-3.8.20-linux-x86_64-gnu                   <download available>
cpython-3.7.9-linux-x86_64-gnu                    <download available>
pypy-3.10.14-linux-x86_64-gnu                     <download available>
pypy-3.9.19-linux-x86_64-gnu                      <download available>
pypy-3.8.16-linux-x86_64-gnu                      <download available>
pypy-3.7.13-linux-x86_64-gnu                      <download available>
Enter fullscreen mode Exit fullscreen mode

You can see the already installed python versions, along the path that each version is installed.
If the python version you specified in the command is already present, this version will be used for executing your python program.

Top comments (0)