# Day 3: Fix a Broken `uv` Lockfile Specification

Welcome to Day 3! Today we are introducing a modern tool called **`uv`**. 

## 🧠 What is `uv` and what is a Lockfile?
While `pip` is the standard, a newer tool named **`uv`** (written in Rust) has become incredibly popular because it is phenomenally faster.

A **Lockfile** (`uv.lock`) is a step above `requirements.txt`. While `requirements.txt` might just say "install pandas", a lockfile records the *exact* versions of pandas, and every sub-dependency pandas relies on, down to the exact file hashes. 

Here is how the workflow looks compared to traditional pip:

```mermaid
flowchart TD
    subgraph Traditional Pip Workflow
        req[requirements.txt] --> pip[pip install]
        pip --> env1[Virtual Environment]
    end

    subgraph Modern uv Workflow
        toml["pyproject.toml<br>High-level deps"] --> uv_resolve["uv lock<br>Resolves all conflicts"]
        uv_resolve --> lock["uv.lock<br>Exact versions & hashes"]
        lock --> uv_sync["uv sync<br>Installs exactly what's in lockfile"]
        uv_sync --> env2[Virtual Environment]
    end
```

---

## 🎯 Task 1: Install `uv`
Let's install this package manager on your Windows machine.

### Step-by-step:
1. Open PowerShell. If your `ml-env` from Day 1/2 is active, you can install it via pip:
   ```powershell
   pip install uv
   ```
2. Verify the installation:
   ```powershell
   uv --version
   ```
   **Expected Output:**
   ```text
   uv 0.1.28
   ```

---

## 🎯 Task 2: Understanding a Broken Specification

### How to reproduce and fix this locally:
1. Create a new folder for a test project and go into it:
   ```powershell
   mkdir C:\Users\HP\Documents\Learning-Content\100-days-ML-challenge\day3-uv-test
   cd C:\Users\HP\Documents\Learning-Content\100-days-ML-challenge\day3-uv-test
   ```
2. Initialize a new `uv` project:
   ```powershell
   uv init
   ```
   **Expected Output:**
   ```text
   Initialized project `day3-uv-test` at `C:\Users\HP\Documents\Learning-Content\100-days-ML-challenge\day3-uv-test`
   ```

3. Let's intentionally "break" it by trying to install two things that conflict.
   ```powershell
   uv add numpy==1.20.0 pandas==2.2.0
   ```
   **Expected Output (Error!):**
   ```text
   error: Cannot resolve dependencies.
   Because `pandas==2.2.0` depends on `numpy>=1.22.4` and you requested `numpy==1.20.0`, `pandas==2.2.0` is incompatible with `numpy==1.20.0`.
   ```
   *(This is what a broken specification looks like!)*

4. **The Fix:** Let `uv` pick the best compatible versions automatically.
   ```powershell
   uv add numpy pandas
   ```
   **Expected Output:**
   ```text
   Resolved 8 packages in 12ms
   Installed 8 packages in 45ms
   ```

---

## 🎯 Task 3: Generating and Syncing the Lockfile

### Step-by-step:
1. Lock the dependencies:
   ```powershell
   uv lock
   ```
   **Expected Output:**
   ```text
   Resolved 8 packages in 5ms
   Updated `uv.lock`
   ```

2. Sync the lockfile into your virtual environment:
   ```powershell
   uv sync
   ```
   **Expected Output:**
   ```text
   Audited 8 packages in 2ms
   Environment is already up-to-date
   ```

---

## 🏁 Wrap Up
By understanding `pyproject.toml`, `uv`, and lockfiles, you ensure your ML environments are rock solid and lightning fast.
