Chapter 1.2: Installing UV & Creating Your First Project

Get UV Running and Build a Data Analysis Project

๐Ÿš€ PROJECT 1.2 | Difficulty: Intermediate | Time: 15 minutes

๐Ÿ“Š Complexity Level: Intermediate โญโญ

Hands-on installation and project setup. Youโ€™ll learn terminal commands and project structureโ€”essential skills for any developer!

๐Ÿ’ป Interactive Options:

  • ๐Ÿ““ Open in JupyterLite - Full Jupyter environment in your browser
  • ๐Ÿ’ก Note: Installing UV requires a terminal, so weโ€™ll show examples but youโ€™ll run this locally!
Note

Special Note: This project is designed for students who have completed the first chapter of CS Quests and are familiar with Python basics. If youโ€™re new to programming, consider starting with the earlier lessons before diving into this project.

Another Special Note: This project requires a local Python environment for full functionality. While we provide examples and code snippets here, some features may not run directly in the browser. For the best experience, consider setting up the project on your own computer.

๐Ÿ“– Installation: Getting UV on Your Computer

UV works on macOS, Linux, and Windows. Letโ€™s get it installed!

Step 1: Install UV

Open your terminal and run:

curl -LsSf https://astral.sh/uv/install.sh | sh

After installation, restart your terminal or run:

source $HOME/.cargo/env

Open PowerShell and run:

powershell -c "irm https://astral.sh/uv/install.ps1 | iex"

Step 2: Verify Installation

Check that UV is installed correctly:

uv --version

You should see something like: uv 0.1.x

โœ… Success! If you see a version number, UV is installed and ready to use!

๐Ÿ—๏ธ Creating Your First Project

Now letโ€™s create a data analysis project. Weโ€™ll analyze student grades and create visualizations!

Initialize a New Project

# Create a new directory and navigate into it
mkdir student-grades-analyzer
cd student-grades-analyzer

# Initialize UV project
uv init

This creates a basic project structure:

student-grades-analyzer/
โ”œโ”€โ”€ pyproject.toml    # Project configuration
โ”œโ”€โ”€ README.md         # Project description
โ””โ”€โ”€ .python-version   # Python version lock

๐Ÿ“ Whatโ€™s pyproject.toml?

This is your projectโ€™s configuration file. It tells UV:

  • What your project is called
  • Which Python version to use
  • Which packages your project depends on

Think of it as your projectโ€™s ID card!

๐Ÿ“ฆ Adding Dependencies

Now letโ€™s add the packages we need for data analysis:

# Add pandas for data manipulation
uv add pandas

# Add matplotlib for creating charts
uv add matplotlib

# Add numpy for numerical operations
uv add numpy

UV will: 1. โšก Download and install each package (super fast!) 2. ๐Ÿ”’ Create a uv.lock file to freeze exact versions 3. ๐Ÿ“‹ Update pyproject.toml with your dependencies

โฑ๏ธ Speed Comparison:

Traditional pip might take 1-2 minutes to install these packages.

UV does it in 5-10 seconds! ๐Ÿš€

๐Ÿ“ Understanding the Project Structure

After adding dependencies, your project looks like this:

student-grades-analyzer/
โ”œโ”€โ”€ .venv/              # Virtual environment (UV creates this)
โ”œโ”€โ”€ pyproject.toml      # Project config with dependencies
โ”œโ”€โ”€ uv.lock             # Locked versions for reproducibility
โ”œโ”€โ”€ README.md
โ””โ”€โ”€ .python-version

๐ŸŽฏ Letโ€™s Write Our First Script

Create a file called analyze_grades.py:

# analyze_grades.py
# Our first UV-managed project!

import pandas as pd
import numpy as np

def create_sample_data():
    """Create sample student grades data"""
    students = ['Alice', 'Bob', 'Charlie', 'Diana', 'Eve']
    
    data = {
        'Student': students,
        'Math': np.random.randint(70, 100, 5),
        'Science': np.random.randint(70, 100, 5),
        'English': np.random.randint(70, 100, 5),
        'History': np.random.randint(70, 100, 5)
    }
    
    return pd.DataFrame(data)

# Create and display the data
df = create_sample_data()
print("๐Ÿ“Š Student Grades:")
print(df)

# Calculate average for each student
df['Average'] = df[['Math', 'Science', 'English', 'History']].mean(axis=1)
print("\n๐Ÿ“ˆ With Averages:")
print(df)

# Find the top student
top_student = df.loc[df['Average'].idxmax()]
print(f"\n๐Ÿ† Top Student: {top_student['Student']} with {top_student['Average']:.2f} average!")

โ–ถ๏ธ Running Your Project

To run your script with UV:

# UV automatically uses the right Python and packages!
uv run python analyze_grades.py

No need to activate a virtual environmentโ€”UV handles it automatically!

๐Ÿ’ก Pro Tip: The uv run command automatically uses your projectโ€™s virtual environment. No more source venv/bin/activate needed!

๐Ÿ”„ Common UV Commands

Here are the most useful UV commands:

# Initialize a new project
uv init

# Add a package
uv add package-name

# Remove a package
uv remove package-name

# Run a script
uv run python script.py

# Show installed packages
uv pip list

# Update all packages
uv lock --upgrade

๐ŸŽฎ Try It Yourself!

  1. Create a new UV project called my-data-project
  2. Add pandas and matplotlib
  3. Create a simple script that prints โ€œHello from UV!โ€
  4. Run it with uv run python your_script.py

๐Ÿ† Challenge:

Modify the analyze_grades.py script to:

  • Add a โ€˜Grade Letterโ€™ column (A for 90+, B for 80+, etc.)
  • Calculate class averages per subject
  • Find which subject has the highest class average

๐Ÿš€ Whatโ€™s Next?

Now that you have UV set up and a working project, in the next slide weโ€™ll:

  • Dive deeper into pandas for data manipulation
  • Load data from files (CSV, Excel)
  • Clean and transform real-world data