Skip to content

Commit 171fab4

Browse files
Improve section 1.3 of the course (#20)
* Improve section 1.3 of the course * review --------- Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> Co-authored-by: Médéric Hurier (Fmind) <[email protected]>
1 parent 8f8d26c commit 171fab4

File tree

1 file changed

+59
-49
lines changed

1 file changed

+59
-49
lines changed

docs/1. Initializing/1.3. uv (project).md

Lines changed: 59 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
description: Discover how to use uv to manage project dependencies and build Python packages, streamlining the process of creating production-ready artifacts.
33
---
44

5-
# 1.3. uv (project)
5+
# 🚀 1.3. uv (project)
66

7-
## What is a package?
7+
## 📦 What is a package?
88

9-
A [Python package](https://packaging.python.org/en/latest/) 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.
9+
A [Python package](https://packaging.python.org/en/latest/) is a collection of Python modules that can be installed and used in your projects. Packages allow you to leverage external libraries and frameworks, extending Python's built-in capabilities.
1010

11-
[uv](https://docs.astral.sh/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:
11+
[uv](https://docs.astral.sh/uv/) simplifies the management of these packages by treating them as dependencies. With uv, you can declare the packages your project needs in a `pyproject.toml` file. uv then ensures that the correct versions of these dependencies are installed, creating a stable and conflict-free development environment. Here’s a basic example of how to specify dependencies with uv:
1212

1313
```toml
1414
# https://docs.astral.sh/uv/reference/pyproject-toml/
@@ -29,9 +29,9 @@ dev = [
2929

3030
You will learn more on how to construct and publish Python Package in the [Package section of this course](../3. Productionizing/3.0. Package.md).
3131

32-
## Why do you need a package manager?
32+
## 🤔 Why do you need a package manager?
3333

34-
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.
34+
In the Python ecosystem, software is typically distributed and installed as packages. These packages, often in Wheel or zip format, contain source code and essential metadata. Manually managing these packages and their dependencies can be a tedious and error-prone process. Package managers like uv automate these tasks, which boosts productivity and ensures that your development and deployment environments are consistent.
3535

3636
<figure markdown="span">
3737
<img src="https://imgs.xkcd.com/comics/python_environment_2x.png" alt="Python Environment" width="500" />
@@ -40,32 +40,31 @@ In the Python ecosystem, the distribution and installation of software through p
4040

4141
By default, uv will download and install Python packages from [PyPI](https://pypi.org/), a repository of software for the Python programming language. If needed, [other Python repositories](https://warehouse.pypa.io/repository-projects/) can be configured to provide extra sources of dependencies.
4242

43-
## Why should you use uv in your project?
43+
## Why should you use uv in your project?
4444

45-
Incorporating uv into your project brings several key advantages:
45+
Using uv in your project offers several key benefits:
4646

47-
- **Improved Environment Management**: uv streamlines the management of different project environments, promoting consistent development practices.
48-
- **Simplified Package Building and Distribution**: It provides a unified workflow for building, distributing, and installing packages, easing the complexities usually associated with these tasks.
49-
- **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.
47+
- **Improved Environment Management**: uv simplifies the management of project environments, which helps maintain consistency.
48+
- **Simplified Package Building and Distribution**: It provides a unified workflow for building, distributing, and installing packages, which reduces complexity.
49+
- **Standardized Project Metadata**: uv uses a standard `pyproject.toml` file to define project metadata, such as dependencies, authors, and versioning. This ensures that your project is easy to understand and maintain.
5050

51-
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.
51+
Compared to traditional tools like pip and venv, uv provides a more cohesive and user-friendly experience by combining package and environment management into a single, streamlined process.
5252

53-
## How can you use uv for your MLOps project?
53+
## 🛠️ How can you use uv for your MLOps project?
5454

55-
Integrating uv into your MLOps project involves several key steps designed to configure and prepare your development environment:
55+
To integrate uv into your MLOps project, you can follow these steps to set up your development environment:
5656

5757
- Begin by creating a new project directory and navigate into it.
5858
- 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.
5959
- 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.
6060

61-
The `pyproject.toml` file plays a central role in defining your projects dependencies and settings.
61+
The `pyproject.toml` file is central to defining your project's dependencies and settings. Let's break down a typical example:
6262

6363
```toml
6464
# https://docs.astral.sh/uv/reference/settings/
6565
# https://packaging.python.org/en/latest/guides/writing-pyproject-toml/
6666

67-
# PROJECT
68-
67+
# Project metadata
6968
[project]
7069
name = "bikes"
7170
version = "4.1.0"
@@ -75,54 +74,45 @@ readme = "README.md"
7574
license = { file = "LICENSE.txt" }
7675
keywords = ["mlops", "python", "package"]
7776
requires-python = ">=3.13"
77+
78+
# Main dependencies
7879
dependencies = [
7980
"loguru>=0.7.3",
8081
"matplotlib>=3.10.1",
8182
"mlflow>=2.20.3",
82-
"numba>=0.61.0",
83-
"numpy>=2.1.3",
84-
"omegaconf>=2.3.0",
85-
"pandas>=2.2.3",
86-
"pandera>=0.23.0",
87-
"plotly>=6.0.0",
88-
"plyer>=2.1.0",
89-
"psutil>=7.0.0",
90-
"pyarrow>=19.0.1",
91-
"pydantic-settings>=2.8.1",
92-
"pydantic>=2.10.6",
93-
"pynvml>=12.0.0",
9483
"scikit-learn>=1.6.1",
95-
"setuptools>=75.8.2",
96-
"shap>=0.46.0",
97-
"hatchling>=1.27.0",
84+
# ... and so on
9885
]
9986

100-
# LINKS
101-
87+
# Project URLs
10288
[project.urls]
10389
Homepage = "https://github.com/fmind/mlops-python-package"
10490
Documentation = "https://fmind.github.io/mlops-python-package/bikes.html"
10591
Repository = "https://github.com/fmind/mlops-python-package"
106-
"Bug Tracker" = "https://github.com/fmind/mlops-python-package/issues"
107-
Changelog = "https://github.com/fmind/mlops-python-package/blob/main/CHANGELOG.md"
108-
109-
# SCRIPTS
11092

93+
# Command-line scripts
11194
[project.scripts]
11295
bikes = 'bikes.scripts:main'
11396

114-
# SYSTEMS
115-
97+
# Build system configuration
11698
[build-system]
11799
requires = ["hatchling"]
118100
build-backend = "hatchling.build"
119101
```
120102

103+
Here’s what each section means:
104+
105+
- **`[project]`**: This section contains general metadata about your project, such as its name, version, and description.
106+
- **`dependencies`**: This is a list of the main dependencies that your project needs to run.
107+
- **`[project.urls]`**: This section allows you to include helpful links, such as to the project's homepage or documentation.
108+
- **`[project.scripts]`**: Here, you can define command-line scripts that will be created when your package is installed.
109+
- **`[build-system]`**: This section specifies the build tool that `uv` will use to create your package.
110+
121111
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.
122112

123-
## How can you install dependencies for your project with uv?
113+
## How can you install dependencies for your project with uv?
124114

125-
Uv differentiates between main (production) and development dependencies, offering an organized approach to dependency management. To add dependencies, use the following commands:
115+
uv distinguishes between main (production) and development dependencies, which allows for a more organized approach to dependency management. To add dependencies, you can use the following commands:
126116

127117
```bash
128118
# For main dependencies
@@ -134,13 +124,13 @@ $ uv add --group dev ipykernel
134124

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

137-
## What is the difference between main and dev dependencies in uv?
127+
## Main vs. Dev Dependencies in uv
138128

139-
In uv, dependencies are divided into two types: [main dependencies](https://docs.astral.sh/uv/concepts/projects/dependencies/#project-dependencies) and [development (dev) dependencies](https://docs.astral.sh/uv/concepts/projects/dependencies/#development-dependencies).
129+
In uv, dependencies are categorized into two types: [main dependencies](https://docs.astral.sh/uv/concepts/projects/dependencies/#project-dependencies) and [development (dev) dependencies](https://docs.astral.sh/uv/concepts/projects/dependencies/#development-dependencies).
140130

141-
**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.
131+
- **Main Dependencies**: These are essential for your project to run in a production environment. For example, libraries like pandas or scikit-learn are typically main dependencies in an MLOps project.
142132

143-
**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.
133+
- **Development Dependencies**: These are only used for development and testing purposes. Examples include testing frameworks like `pytest` or linters like `ruff`. They are not required for the application to run in production.
144134

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

@@ -158,11 +148,31 @@ dev = [
158148

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

161-
## Can you use uv to download Python dependencies from your own organization's code repository?
151+
## 🏢 Using Custom Repositories
152+
153+
uv supports [custom package repositories](https://docs.astral.sh/uv/concepts/projects/dependencies/#dependency-sources), including private and organizational ones. This allows you to use proprietary packages alongside those from the public PyPI. You can easily add a custom repository and configure authentication using uv's commands, which ensures secure and flexible dependency management.
154+
155+
## ⚡ Common `uv` Commands
156+
157+
Here is a quick reference for some of the most common `uv` commands:
158+
159+
- **`uv init`**: Initializes a new project by creating a `pyproject.toml` file.
160+
- **`uv add`**: Adds a new dependency to your project.
161+
- **`uv sync`**: Installs the dependencies listed in your `pyproject.toml` file.
162+
- **`uv run`**: Executes a command in the project's virtual environment.
163+
164+
## 🔒 The `uv.lock` File
165+
166+
The `uv.lock` file is generated after you install your project's dependencies. It records the exact versions of all the packages that were installed, including their dependencies. This is crucial for ensuring that your project is reproducible. When you share your project with others, they can use the `uv.lock` file to create an identical environment, which helps to avoid bugs and inconsistencies.
167+
168+
## 🔑 Key Takeaways
162169

163-
Uv supports incorporating [custom package repositories, including private or organizational ones](https://docs.astral.sh/uv/concepts/projects/dependencies/#dependency-sources). 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.
170+
- **Unified Tool**: `uv` streamlines Python project management by combining the functionalities of `pip`, `venv`, `pipx`, and `pyenv` into a single, fast tool.
171+
- **`pyproject.toml`**: This file is central to `uv` projects, defining metadata, dependencies (main and development), and build configurations.
172+
- **Dependency Management**: `uv` simplifies adding, managing, and synchronizing project dependencies, ensuring consistent environments.
173+
- **Reproducibility**: The `uv.lock` file guarantees reproducible environments by locking exact dependency versions.
164174

165-
## uv (project) additional resources
175+
## 📚 Additional Resources
166176

167177
- **[`pyproject.toml` example from the MLOps Python Package](https://github.com/fmind/mlops-python-package/blob/main/pyproject.toml)**
168178
- [uv: Unified Python packaging](https://astral.sh/blog/uv-unified-python-packaging)

0 commit comments

Comments
 (0)