Published at

Getting Started with UV: A Modern Package Manager for Python

Getting Started with UV: A Modern Package Manager for Python

A quick guide to getting started with UV, a modern Python package manager. Covers installation, adding dependencies, generating requirements.txt, and a Dockerfile example.

Authors
  • avatar
    Name
    James Lau
    Twitter
  • Indie App Developer at Self-employed
Sharing is caring!
Table of Contents

UV is a fast, modern package manager and resolver for Python. This post will guide you through the basic usage of UV, covering initialization, synchronization, adding dependencies, and generating requirements files. We’ll also explore a VS Code preset and a Dockerfile example to streamline your development workflow.

Project Setup

First, initialize a new project using UV:

uv init myproject

This command creates the project directory myproject.

Syncing the Environment

To synchronize your environment with the project’s dependencies, use:

uv sync

Adding Dependencies

To add a development dependency, for example, requests, use the following command:

uv add --dev requests

This command adds requests as a development dependency to your project.

VS Code Preset

For a more streamlined VS Code experience, consider using the provided VS Code preset. You can download and extract the .vscode.zip file and place the .vscode folder in the root of your project. This preset likely includes configurations for linting, formatting, and debugging, optimized for use with UV.

Generating requirements.txt

UV can generate a requirements.txt file from your pyproject.toml:

uv pip compile pyproject.toml -o requirements.txt

This is useful for compatibility with tools that rely on requirements.txt.

Dockerfile Example

Here’s a Dockerfile example demonstrating how to use UV within a container. This example uses a multi-stage build to include uv and create a virtual environment.

# Use a multi-stage build to first get uv
FROM ghcr.io/astral-sh/uv:0.2.12 as uv
# Choose your python version here
FROM python:3.10.1-slim-buster
# Create a virtual environment with uv inside the container
RUN --mount=from=uv,source=/uv,target=./uv     ./uv venv /opt/venv
# We need to set this environment variable so that uv knows where
# the virtual environment is to install packages
ENV VIRTUAL_ENV=/opt/venv
# Make sure that the virtual environment is in the PATH so
# we can use the binaries of packages that we install such as pip
# without needing to activate the virtual environment explicitly
ENV PATH="/opt/venv/bin:$PATH"

WORKDIR /app

# Copy the requirements file into the container
COPY requirements.txt .
# Install the packages with uv using --mount=type=cache to cache the downloaded packages
RUN --mount=type=cache,target=/root/.cache/uv     --mount=from=uv,source=/uv,target=./uv     ./uv pip install  -r requirements.txt

COPY . .

EXPOSE 8000

CMD ["uvicorn", "lyricsair_backend.asgi:application", "--host", "0.0.0.0", "--port", "8000"]
#CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]

Explanation:

  1. Multi-stage Build: The Dockerfile uses a multi-stage build. The first stage (uv) retrieves the uv executable from ghcr.io/astral-sh/uv:0.2.12.
  2. Base Image: It starts from a slim Python 3.10 image.
  3. Virtual Environment: It creates a virtual environment using uv venv /opt/venv.
  4. Environment Variables: It sets VIRTUAL_ENV and updates the PATH to use the virtual environment.
  5. Dependencies Installation: It copies the requirements.txt file and installs the dependencies using uv pip install -r requirements.txt. The --mount=type=cache directive caches the downloaded packages to speed up subsequent builds.
  6. Copying Source Code: The source code is copied to the /app directory.
  7. Exposing Port: The container exposes port 8000.
  8. Command: Finally, it defines the command to run the application using uvicorn.

This Dockerfile provides a solid foundation for deploying Python applications using UV.

Sharing is caring!