What is UV?

An extremely fast Python all in one package manager written in Rust. It replaces the following tools and is extremely fast.

  • pip
  • pip-tools
  • pipx
  • poetry
  • pyenv
  • virtualenv

Its one tool to rule them all.


Install UV

Mac and Linux

curl -LsSf https://astral.sh/uv/install.sh | sh

windows

powershell -c "irm https://astral.sh/uv/install.ps1 | iex"

Via Python

pip install uv

Manually installing Python version using UV

To download a python versions. This installs python which can be used by uv project and does not mess up your global python

uv python install 3.12

To see all installed version of python by uv

uv python list --only-installed

This step is optional but gives user the flexibility to install any version of python they need.


Initializing UV

To start we initialize a project with init

# if you have a project folder created
uv init 

This creates apyproject.toml the new standard for building python application. Read more Writing your pyproject.toml

[project]
name = "my_python_poject"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.13"
dependencies = []

and a. .python-version which specifies the version of python for this project.

You can also init by specifying the python version and project name. here python version is 3.13 and project name is my_python_poject

uv init --python 3.13 my_python_poject

Creating a virtual env

UV by default will activate the virtual env when called using uv run however if we want to create one before we can use

uv venv

Create a venv with required version

uv venv --python 3.13

The venv needs to be manually activated if we are not using uv

source .venv/bin/activate # linux or mac
source .venv\Scripts\activate # windows

Installing and removing dependencies

Installing Dependencies

To install package uv downloads and ensures no conflict and add it to pyproject.toml

uv add pytest requests

your pyproject.toml will look like below post add

[project]
name = "my_python_poject"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.13"
dependencies = [
    "pytest>=8.3.4",
    "requests>=2.32.3",
]

if you have a requirements.txt then

uv add -r requirements.txt

The below also works but I prefer the uv add command

uv pip install -r requirements.txt

You can also add dependencies directly to pyproject.toml then run this will also sync any dev dependencies

We can add dev dependencies like linter or formatter by passing the group name here it is dev

uv add --dev ruff

Uninstalling Dependencies

uv remove pytest

or delete the particular package from pyproject.toml and run uv sync

List all dependencies installed

To see all package and its dependencies

uv tree

Sync dependencies to other

Once a project is created using uv and packages installed. uv creates a uv.lock this should be synced with git

If someone clones your repo they can get an exact replica of your environment including the python version by running the below command

uv sync

To sync other dependencies except dev dependencies

uv sync --no-group dev

Running program with uv

To run a program use uv run this activates the virtual env and runs the program

uv run main.py

Sharing code snippets

At times we may have a small program we want to share but this need python to be installed and the required packages uv solves this using --with

As long as uv is installed and the dependencies are mentioned in --with uv will create the virtual env and install the dependencies and run the code.

uv run --with requests demo.py

or we can add incline scripts by adding below code for script we do not need --with mentioned here

# /// script
# requires-python = ">=3.12"
# dependencies = [
#     "requests",
# ]
# ///
import requests

result = requests.get("https://www.google.com")
print(result.status_code)
uv run google_status.py

A shortcut to add script automatically by uv is to use the --script keyword and pass the required dependencies after the script

uv add --script demo.py 'requests'