Uv

Introduction

  • An extremely fast Python package and project manager, written in Rust.

  • A single tool to replace pip, pip-tools, pipx, poetry, pyenv, twine, virtualenv, and more.

Install & manage python version

  • uv python install: Install Python versions.

  • uv python list: View available Python versions.

uv python install 3.12

Create New Project & Virtual environment

uv init <project_name>
uv venv --python 3.12
source .venv/bin/activate

uv will create the following files:

.
├── .python-version
├── README.md
├── main.py
└── pyproject.toml

  • The pyproject.toml contains metadata about your project:

  • It is similar with package.json

[project]
name = "hello-world"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
dependencies = []

uv.lock

  • uv.lock is a cross-platform lockfile that contains exact information about your project's dependencies. Unlike the pyproject.toml which is used to specify the broad requirements of your project, the lockfile contains the exact resolved versions that are installed in the project environment. This file should be checked into version control, allowing for consistent and reproducible installations across machines.

  • Similar with package-lock.json

.python-version

The .python-version file contains the project's default Python version. This file tells uv which Python version to use when creating the project's virtual environment.

Managing Dependencies

  • There are many ways to install package, after adding package, pyproject.toml and uv.lock will be updated

  • uv is compatible to pip

uv add <package>
# install exact version
uv add <package==0.20>
# install based on pip files
uv add -r requirements.txt
# install from pip
uv pip install <package>
uv pip list
# ensure all the package are installed based on uv.lock file
uv sync

Tool

  • Tools are Python packages that provide command-line interfaces.

  • Similar with npm install <package> -g

uv tool install ruff==0.5.0
ruff --version
// similar with npx 
uvx ruff --version

Docker file

# Use a Python image with uv pre-installed
FROM ghcr.io/astral-sh/uv:python3.12-bookworm-slim

# Install the project into `/app`
WORKDIR /app

# Enable bytecode compilation
ENV UV_COMPILE_BYTECODE=1

# Copy from the cache instead of linking since it's a mounted volume
ENV UV_LINK_MODE=copy

# Install the project's dependencies using the lockfile and settings
RUN --mount=type=cache,target=/root/.cache/uv \
    --mount=type=bind,source=uv.lock,target=uv.lock \
    --mount=type=bind,source=pyproject.toml,target=pyproject.toml \
    uv sync --frozen --no-install-project --no-dev

# Then, add the rest of the project source code and install it
# Installing separately from its dependencies allows optimal layer caching
COPY . /app
RUN --mount=type=cache,target=/root/.cache/uv \
    uv sync --frozen --no-dev

ENV PATH="/app/.venv/bin:$PATH"


EXPOSE 80
ENV PORT 80

ENV LLM_MODEL openrouter/openai/gpt-4o-mini
ENV OPENROUTER_API_KEY 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
ENV GOOGLE_API_KEY AIzaSyBUguvUNCxqha21BhH6r3bOcF48ckoxTz8
ENV CHROMA_HOST 34.44.52.123
ENV CHROMA_PORT 8000

# ENTRYPOINT ["tail", "-f", "/dev/null"]
CMD ["fastapi", "run", "main.py", "--port", "80"]

Last updated

Was this helpful?