A while ago, I wrote a post about using poetry as a python package manager. Today, I want to introduce you to another package manager called uv.

What is UV?

According to Astral, the author of uv, uv is an extremely fast Python package installer and resolver, written in Rust, and designed as a drop-in replacement for pip and pip-tools workflows.

uv represents a milestone in our pursuit of a “Cargo for Python”: a comprehensive Python project and package manager that’s fast, reliable, and easy to use.

png

Setting Up uv

1 - Installation

pip install uv
pipx install uv

2 - Creating a virtual environment

uv venv --python 3.12

3 - Activating the virtual environment

source .venv/bin/activate

4 - Managing packages

First, we will need a pyproject.toml file. Here is an example of a pyproject.toml file:

[project]
name = "my-uv-project"
version = "0.1.0"
description = "This is a project that uses uv as a package manager."
readme = "README.md"
requires-python = ">=3.12, <3.13"
dependencies = [
    "ruff>=0.2.0",
]

If your project need to use any of the private github package, then we can add the following to the pyproject.toml file:

[project]
name = "my-uv-project"
version = "0.1.0"
description = "This is a project that uses uv as a package manager."
readme = "README.md"
requires-python = ">=3.12, <3.13"
dependencies = [
    "ruff>=0.2.0",
]

[tool.ruff.lint]
extend-select = ["I"]

[tool.uv.sources]
private-lib = { git = "ssh://git@github.com/fishwongy/private-lib.git" }
private-repo = { git = "ssh://git@github.com/fishwongy/private-repo.git" }
uv pip install -r pyproject.toml

5 - Deploying the project

Dockerfile without private repo

FROM --platform=linux/amd64 python:3.12-slim

WORKDIR /usr/src/app

ENV PYTHONUNBUFFERED=1 \
    PYTHONDONTWRITEBYTECODE=1 \
    UV_VERSION=0.4.4


ENV PATH="/root/.local/bin:$PATH"

RUN pip install pipx
RUN pipx install uv==${UV_VERSION}

COPY app .

CMD ["uv", "run", "python3", "main.py"]

Dockerfile with private repo

FROM --platform=linux/amd64 python:3.12-slim

WORKDIR /usr/src/app

ENV PYTHONUNBUFFERED=1 \
    PYTHONDONTWRITEBYTECODE=1 \
    UV_VERSION=0.4.4

RUN apt update -y && apt install -y ssh git wget unzip libaio1 && \
    apt-get clean

ENV PATH="/root/.local/bin:$PATH"

RUN pip install pipx
RUN pipx install uv==${UV_VERSION}

COPY ./uv.lock pyproject.toml /usr/src/app
RUN ssh-keyscan github.com >> /root/.ssh/known_hosts
RUN --mount=type=ssh uv sync -vvv

COPY app .

CMD ["uv", "run", "python3", "main.py"]

skaffold.yaml for ssh

apiVersion: skaffold/v2beta28
kind: Config
metadata:
  name: my-uv-app
build:
  local:
    useBuildkit: true
  artifacts:
  - image: docker.io/fishwongy/my-uv-app
    docker:
      dockerfile: Dockerfile
      ssh: default

deploy: {}

profiles:
  - name: production
    activation:
      - kubeContext: prod
    deploy:
      kustomize:
        paths:
          - ./deploy/production

  - name: staging
    activation:
      - kubeContext: stg
    deploy:
      kustomize:
        paths:
          - ./deploy/staging

  - name: development
    activation:
      - kubeContext: dev
    deploy:
      kustomize:
        paths:
          - ./deploy/development

That’s it for now.

Thank you for reading and have a nice day!