Skip to content

Commit ee1ac82

Browse files
committed
Added uv
1 parent 10ae36a commit ee1ac82

File tree

6 files changed

+306
-5
lines changed

6 files changed

+306
-5
lines changed

docs/1. Initializing/1.2. uv.md

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
---
2+
description: Discover uv, a fast and versatile Python package manager and project manager designed to streamline your development workflow. Learn how to install it and use it as a drop-in replacement for pip, venv, pipx, and pyenv.
3+
---
4+
5+
# 1.2. uv
6+
7+
## What is uv?
8+
9+
[uv](https://docs.astral.sh/uv/) is an extremely fast Python package installer and resolver, written in Rust, designed to be a drop-in replacement for `pip`, `pipx`, `venv`, and `pyenv`. Created by [Astral](https://astral.sh/), the same team behind the high-performance linter [Ruff](https://docs.astral.sh/ruff/), `uv` aims to significantly speed up and simplify Python project management. It offers a unified toolchain that handles virtual environments, dependency resolution, package installation, and more, all while providing exceptional performance.
10+
11+
## Why should you use uv?
12+
13+
`uv` offers several compelling advantages for Python developers, especially in the context of MLOps:
14+
15+
- **Performance**: `uv` is incredibly fast, often outperforming `pip`, `venv`, and `pyenv` by a significant margin. This speed translates to faster project setup, quicker dependency resolution, and reduced wait times during development and deployment.
16+
- **Unified Toolchain**: `uv` replaces multiple tools, simplifying your development workflow. It can manage virtual environments, install packages, and resolve dependencies, all within a single command-line interface.
17+
- **Drop-in Replacement**: `uv` is designed to be a drop-in replacement for common Python tools. This means you can often substitute `uv` commands for `pip`, `venv`, or `pipx` commands without altering your existing workflows significantly.
18+
- **Active Development**: Backed by Astral, `uv` is under active development with a focus on performance, reliability, and ease of use.
19+
- **Cross-Platform Compatibility**: `uv` works seamlessly across Linux, macOS, and Windows, ensuring a consistent experience regardless of your operating system.
20+
- **Caching**: `uv` implements aggressive caching mechanisms to avoid redundant work, further speeding up operations like package installation and environment setup.
21+
22+
## How to install uv?
23+
24+
[Installing `uv` is straightforward](https://docs.astral.sh/uv/getting-started/installation/). The recommended method is to use the official installation script, which automatically detects your operating system and installs the appropriate version:
25+
26+
```bash
27+
curl -LsSf https://astral.sh/uv/install.sh | sh
28+
```
29+
30+
Alternatively, you can install `uv` using `pip` or `pipx`:
31+
32+
```bash
33+
pip install uv
34+
# or
35+
pipx install uv
36+
```
37+
38+
Once installed, verify the installation by checking the version:
39+
40+
```bash
41+
uv --version
42+
```
43+
44+
## How to use uv as a drop-in replacement for pip?
45+
46+
`uv` can be used as a direct replacement for many common `pip` commands. Here's how:
47+
48+
- **Installing packages**:
49+
50+
```bash
51+
uv pip install requests numpy pandas
52+
```
53+
54+
- **Uninstalling packages**:
55+
56+
```bash
57+
uv pip uninstall requests
58+
```
59+
60+
- **Listing installed packages**:
61+
62+
```bash
63+
uv pip freeze
64+
```
65+
66+
- **Updating packages**:
67+
68+
```bash
69+
uv pip install --upgrade requests
70+
```
71+
72+
- **Installing packages from a `requirements.txt` file**:
73+
74+
```bash
75+
uv pip install -r requirements.txt
76+
```
77+
78+
## How to use uv as a drop-in replacement for venv?
79+
80+
`uv` can also replace `venv` for creating and managing virtual environments:
81+
82+
- **Creating a virtual environment**:
83+
84+
```bash
85+
uv venv
86+
```
87+
88+
This command creates a new virtual environment in the `.venv` directory by default. You can customize the location using the `--python` flag to specify a Python interpreter path.
89+
90+
- **Activating the virtual environment**:
91+
92+
The activation process depends on your shell. For example, on bash:
93+
94+
```bash
95+
source .venv/bin/activate
96+
```
97+
98+
- **Listing available Python interpreters**:
99+
100+
```bash
101+
uv venv --python
102+
```
103+
104+
## How to use uv as a drop-in replacement for pipx?
105+
106+
`uv` can also replace `pipx` for installing and managing globally available Python tools:
107+
108+
- **Installing a tool globally**:
109+
110+
```bash
111+
uv tool install ruff
112+
```
113+
114+
- **Listing globally installed tools**:
115+
116+
```bash
117+
uv tool list
118+
```
119+
120+
- **Running a tool without installing it**:
121+
122+
```bash
123+
uv tool run ruff --version
124+
```
125+
126+
## How to install a Python version with uv?
127+
128+
`uv` can also be used to install specific Python versions, similar to `pyenv`. This is particularly useful when you need to test your code against different Python environments or when a project requires a specific Python version that is not your system's default.
129+
130+
- **Installing a specific Python version**:
131+
132+
```bash
133+
uv python install 3.12
134+
```
135+
136+
This command downloads and installs Python 3.12. You can then use this version to create virtual environments or run scripts.
137+
138+
- **Listing available Python versions**:
139+
140+
```bash
141+
uv python list
142+
```
143+
144+
- **Listing installed Python versions**:
145+
146+
```bash
147+
uv python list --only-installed
148+
```
149+
150+
- **Removing a specific Python version**:
151+
152+
```bash
153+
uv python remove 3.12
154+
```
155+
156+
## uv additional resources
157+
158+
- **[uv Documentation](https://docs.astral.sh/uv/)**: The official documentation provides comprehensive information on all `uv` features and commands.
159+
- [uv Installation](https://docs.astral.sh/uv/getting-started/installation/)
160+
- [uv Features](https://docs.astral.sh/uv/getting-started/features/)
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
---
2+
description: Discover how to use uv to manage project dependencies and build Python packages, streamlining the process of creating production-ready artifacts.
3+
---
4+
5+
# 1.3. uv (project)
6+
7+
## What is a package?
8+
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.
10+
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:
12+
13+
```toml
14+
# https://docs.astral.sh/uv/reference/pyproject-toml/
15+
16+
[project]
17+
name = "example-project"
18+
version = "0.1.0"
19+
description = "An example project to demonstrate uv"
20+
dependencies = [
21+
"requests>=2.32.3",
22+
]
23+
24+
[project.optional-dependencies]
25+
dev = [
26+
"pytest>=8.3.4",
27+
]
28+
```
29+
30+
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).
31+
32+
## Why do you need a package manager?
33+
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.
35+
36+
<figure markdown="span">
37+
<img src="https://imgs.xkcd.com/comics/python_environment_2x.png" alt="Python Environment" width="500" />
38+
<figcaption>Python Environment(<a href="https://xkcd.com/1987/">source</a>)</figcaption>
39+
</figure>
40+
41+
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.
42+
43+
## Why should you use uv in your project?
44+
45+
Incorporating uv into your project brings several key advantages:
46+
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.
50+
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.
52+
53+
## How can you use uv for your MLOps project?
54+
55+
Integrating uv into your MLOps project involves several key steps designed to configure and prepare your development environment:
56+
57+
- Begin by creating a new project directory and navigate into it.
58+
- 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.
59+
- 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.
60+
61+
The `pyproject.toml` file plays a central role in defining your project’s dependencies and settings.
62+
63+
```toml
64+
# https://docs.astral.sh/uv/reference/pyproject-toml/
65+
66+
[project]
67+
name = "bikes"
68+
version = "1.0.0"
69+
description = "Predict the number of bikes available."
70+
authors = [{ name = "Your Name", email = "Your Email" }]
71+
readme = "README.md"
72+
requires-python = ">=3.12"
73+
dependencies = []
74+
license = { file = "LICENSE.txt" }
75+
keywords = ["mlops", "python", "package"]
76+
77+
[project.urls]
78+
Homepage = "https://github.com/fmind/bikes"
79+
Documentation = "https://fmind.github.io/bikes/"
80+
Repository = "https://github.com/fmind/bikes"
81+
"Bug Tracker" = "https://github.com/fmind/bikes/issues"
82+
Changelog = "https://github.com/fmind/bikes/blob/main/CHANGELOG.md"
83+
84+
[project.scripts]
85+
bikes = 'bikes.scripts:main'
86+
87+
[build-system]
88+
requires = ["hatchling"]
89+
build-backend = "hatchling.build"
90+
```
91+
92+
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.
93+
94+
## How can you install dependencies for your project with uv?
95+
96+
Uv differentiates between main (production) and development dependencies, offering an organized approach to dependency management. To add dependencies, use the following commands:
97+
98+
```bash
99+
# For main dependencies
100+
$ uv add pandas scikit-learn
101+
102+
# For development dependencies
103+
$ uv add --group dev ipykernel
104+
```
105+
106+
Executing these commands updates the `pyproject.toml` file, accurately managing and versioning your project's dependencies.
107+
108+
## What is the difference between main and dev dependencies in uv?
109+
110+
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).
111+
112+
**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.
113+
114+
**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.
115+
116+
Here’s a simple example in a `pyproject.toml` file:
117+
118+
```toml
119+
[project]
120+
dependencies = [
121+
"flask>=3.1.0", # Main dependency
122+
]
123+
124+
[project.optional-dependencies]
125+
dev = [
126+
"pytest>=8.3.4", # Development dependency
127+
]
128+
```
129+
130+
This setup helps keep production environments lean by excluding unnecessary development tools.
131+
132+
## Can you use uv to download Python dependencies from your own organization's code repository?
133+
134+
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.
135+
136+
## uv (project) additional resources
137+
138+
- **[`pyproject.toml` example from the MLOps Python Package](https://github.com/fmind/mlops-python-package/blob/main/pyproject.toml)**
139+
- [uv: Unified Python packaging](https://astral.sh/blog/uv-unified-python-packaging)

docs/1. Initializing/1.2. pyenv.md renamed to docs/1. Initializing/1.7. pyenv (ARCHIVE).md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
description: Learn how to use pyenv to manage Python versions in MLOps projects and ensure that each project uses the correct Python version.
33
---
44

5-
# 1.2. pyenv
5+
# 1.7. pyenv (ARCHIVE)
66

77
## What is pyenv?
88

docs/1. Initializing/1.3. Poetry.md renamed to docs/1. Initializing/1.8. Poetry (ARCHIVE).md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
description: Discover how to use Poetry to manage project dependencies and build Python packages, streamlining the process of creating production-ready artifacts.
33
---
44

5-
# 1.3 Poetry
5+
# 1.8 Poetry (ARCHIVE)
66

77
## What is Poetry?
88

docs/1. Initializing/index.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ The initialization phase is crucial in setting the stage for efficient and strea
88

99
- **[1.0. System](./1.0. System.md)**: This section ensures your system is adequately prepared, outlining the essential prerequisites for installing and effectively running the necessary development tools.
1010
- **[1.1. Python](./1.1. Python.md)**: Here, we introduce how to set up Python—the core programming language for our projects. We'll focus on version management and creating isolated environments for each project to avoid conflicts and dependency issues.
11-
- **[1.2. pyenv](./1.2. pyenv.md)**: We explore `pyenv`, a tool for managing multiple Python versions. It's invaluable for working on various projects that each require different Python environments.
12-
- **[1.3. Poetry](./1.3. Poetry.md)**: This part delves into using `Poetry` for dependency management and project packaging. It simplifies the process of defining, installing, and updating project dependencies with ease.
11+
- **[1.2. uv](./1.2. uv.md)**: We explore `uv`, an extremely fast Python tool written in Rust. Uv can install Python versions, manage virtual environments, and handle dependencies, making it a versatile tool for MLOps projects.
12+
- **[1.3. uv (project)](./1.3. uv (project).md)**: This part delves into using `uv` for project packaging. It simplifies the process of defining, installing, and updating project metadata and dependencies with ease.
1313
- **[1.4. git](./1.4. git.md)**: Focuses on `git`, the cornerstone version control system integral to GitHub. You'll learn how to initiate and manage repositories effectively, a critical skill for collaborative development.
1414
- **[1.5. GitHub](./1.5. GitHub.md)**: Discusses how to leverage GitHub for project hosting, version control, and collaboration. It's a pivotal component in modern development workflows, facilitating teamwork and project management.
1515
- **[1.6. VS Code](./1.6. VS Code.md)**: Highlights the setup of Visual Studio Code (VS Code), showing how to adapt this versatile editor into an integrated development environment (IDE) customized for Python and MLOps projects.
16+
- **[1.7. pyenv (ARCHIVE)](./1.7. pyenv (ARCHIVE).md)**: Explores `pyenv`, a Python version management tool that simplifies handling multiple Python versions on a single machine. It ensures each project uses the correct Python version, preventing conflicts and compatibility issues.
17+
- **[1.8. Poetry (ARCHIVE)](./1.8. Poetry (ARCHIVE).md)**: Introduces `Poetry`, a Python dependency management tool that simplifies package installation and project configuration. You'll learn how to create, manage, and publish Python packages using Poetry.

docs/3. Productionizing/3.0. Package.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ Although [Conda](https://conda.io/projects/conda/en/latest/user-guide/install/in
5151

5252
## How can you install new dependencies with uv?
5353

54-
Please refer to [this section of the course](../1. Initializing/1.3. uv.md).
54+
Please refer to [this section of the course](../1. Initializing/1.3. uv (project).md).
5555

5656
## Which metadata should you provide to your Python package?
5757

0 commit comments

Comments
 (0)