Skip to content

1.3. uv (project)

What is a package?

A Python package is a set of Python modules grouped together that can be installed and used within your projects. Python packages help you manage the functionality of Python by allowing you to add and utilize external libraries and frameworks that are not part of the standard Python library.

uv simplifies the management of these packages by handling them as dependencies. When using uv, developers can easily specify which packages are needed for their projects through a pyproject.toml file. Uv ensures that all specified dependencies are installed in the correct versions, maintaining a stable and conflict-free environment for development. Here’s an example of specifying dependencies with uv:

# https://docs.astral.sh/uv/reference/pyproject-toml/

[project]
name = "example-project"
version = "0.1.0"
description = "An example project to demonstrate uv"
dependencies = [
    "requests>=2.32.3",
]

[project.optional-dependencies]
dev = [
    "pytest>=8.3.4",
]

You will learn more on how to construct and publish Python Package in the Package section of this course.

Why do you need a package manager?

In the Python ecosystem, the distribution and installation of software through packages is a standard practice. These packages, often available in Wheel or zip formats, encapsulate source code along with vital metadata. Manually handling these packages and their dependencies can quickly become cumbersome, underscoring the need for package managers. Tools like uv automate these processes, boosting productivity and guaranteeing consistent environments across development and deployment.

Python Environment
Python Environment(source)

By default, uv will download and install Python packages from PyPI, a repository of software for the Python programming language. If needed, other Python repositories can be configured to provide extra sources of dependencies.

Why should you use uv in your project?

Incorporating uv into your project brings several key advantages:

  • Improved Environment Management: uv streamlines the management of different project environments, promoting consistent development practices.
  • Simplified Package Building and Distribution: It provides a unified workflow for building, distributing, and installing packages, easing the complexities usually associated with these tasks.
  • Uniform Project Metadata: uv employs a standardized approach to defining project metadata, including dependencies, authors, and versioning, through a pyproject.toml file. This standardization ensures clarity and uniformity.

Compared to traditional approaches that involve pip, venv, and manual dependency management, uv offers a more cohesive and friendly experience, merging multiple package and environment management tasks into a single, simplified process.

How can you use uv for your MLOps project?

Integrating uv into your MLOps project involves several key steps designed to configure and prepare your development environment:

  • Begin by creating a new project directory and navigate into it.
  • Run uv init in your terminal. This command starts an interactive guide to help set initial project parameters, such as package name, version, description, author, and dependencies. This step generates a pyproject.toml file, crucial for your project's configuration under uv.
  • Run uv sync to install the project dependencies and source code. This will let you access your project code through uv run and its command-line utilities.

The pyproject.toml file plays a central role in defining your project’s dependencies and settings.

# https://docs.astral.sh/uv/reference/pyproject-toml/

[project]
name = "bikes"
version = "1.0.0"
description = "Predict the number of bikes available."
authors = [{ name = "Your Name", email = "Your Email" }]
readme = "README.md"
requires-python = ">=3.12"
dependencies = []
license = { file = "LICENSE.txt" }
keywords = ["mlops", "python", "package"]

[project.urls]
Homepage = "https://github.com/fmind/bikes"
Documentation = "https://fmind.github.io/bikes/"
Repository = "https://github.com/fmind/bikes"
"Bug Tracker" = "https://github.com/fmind/bikes/issues"
Changelog = "https://github.com/fmind/bikes/blob/main/CHANGELOG.md"

[project.scripts]
bikes = 'bikes.scripts:main'

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

At the end of the installation process, a uv.lock file is generated with all the project dependencies that have been installed. You can remove and regenerate the uv.lock file if you wish to update the list of dependencies.

How can you install dependencies for your project with uv?

Uv differentiates between main (production) and development dependencies, offering an organized approach to dependency management. To add dependencies, use the following commands:

# For main dependencies
$ uv add pandas scikit-learn

# For development dependencies
$ uv add --group dev ipykernel

Executing these commands updates the pyproject.toml file, accurately managing and versioning your project's dependencies.

What is the difference between main and dev dependencies in uv?

In uv, dependencies are divided into two types: main dependencies and development (dev) dependencies.

Main Dependencies: These are essential for your project's production environment—your application can't run without them. For example, libraries like Pandas or XGBoost would be main dependencies for an MLOps project.

Development Dependencies: These are used only during development and testing, such as testing frameworks (e.g., pytest) or linters (e.g., ruff). They are not required in production.

Here’s a simple example in a pyproject.toml file:

[project]
dependencies = [
    "flask>=3.1.0",  # Main dependency
]

[project.optional-dependencies]
dev = [
    "pytest>=8.3.4",  # Development dependency
]

This setup helps keep production environments lean by excluding unnecessary development tools.

Can you use uv to download Python dependencies from your own organization's code repository?

Uv supports incorporating custom package repositories, including private or organizational ones. This capability allows for the use of proprietary packages in conjunction with those from the public PyPI. Adding a custom repository and setting up authentication is facilitated by uv's configuration commands, offering secure and adaptable dependency management.

uv (project) additional resources