← Back to Development Setup

📦 UV & Python Environments

Learn about virtual environments and UV - the modern Python package manager.

Why Virtual Environments?

Isolated projects: Each project gets its own Python packages

No conflicts: Different versions for different projects

Easy sharing: Others can recreate your exact setup

The Problem Without Virtual Environments

Without Virtual Env

All packages installed globally on your computer

  • Project A needs pandas v1.5
  • Project B needs pandas v2.0
  • They conflict! One breaks.
  • Hard to share exact setup with others

With Virtual Env

Each project has its own isolated environment

  • Project A: pandas v1.5 in its own env
  • Project B: pandas v2.0 in its own env
  • No conflicts! Both work perfectly.
  • Share requirements.txt for exact setup

What is UV?

UV: Modern Python Package Manager

Created by Astral, UV is a blazingly fast replacement for pip and virtualenv.

Read the official UV documentation
🚀

10-100x Faster

Written in Rust for speed

🎯

Simple Commands

Easier than pip + venv

🔒

Reliable

Consistent installs every time

Why UV instead of pip? UV handles both Python installation AND package management in one tool. It's faster, simpler, and recommended for modern Python development.

Installing UV

🍎🐧

Mac & Linux

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

Windows (PowerShell)

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

✓ Verify Installation

Restart your terminal, then check:

$uv --version
uv 0.4.0

Essential UV Commands

UV uses a modern, simple workflow. Here are the commands you'll use:

uv init

Initialize a new Python project with a virtual environment

$uv init my-project --python 3.12
Initialized project `my-project` at `/path/to/my-project`

What happens: Creates a project folder with pyproject.toml (project config) and sets up a virtual environment with Python 3.12 automatically.

Important: Always use --python 3.12 for this course. Python 3.13 has compatibility issues with some data sources (Zenodo).

uv add

Add a package to your project (installs it automatically)

$uv add jupyter numpy pandas matplotlib seaborn
Resolved 5 packages in 320ms Downloaded 5 packages in 1.2s Installed 5 packages in 150ms Updated pyproject.toml

What happens: Installs the package AND adds it to your pyproject.toml so others can recreate your setup.

uv sync

Install all packages from an existing project

$uv sync
Resolved 23 packages in 450ms Installed 23 packages in 2.1s

When to use: When you clone a project (like our course repo) that already has a pyproject.toml file. This installs everything needed.

Pro tip: When you open the project in Cursor IDE, it will automatically detect and use your virtual environment. No manual activation needed!

🎯 Workflow: Setting Up the Course Project

Here's what you'll do to set up the course notebooks:

1

Clone the course repository

$ git clone https://github.com/HocheggerLab/y3-bio-python.git
2

Navigate into the project

$ cd y3-bio-python
3

Sync all course packages

$ uv sync

This reads pyproject.toml and installs jupyter, numpy, pandas, matplotlib, biopython, etc.

4

Open in Cursor IDE

Open the project folder in Cursor IDE:

$ cursor .

Cursor will automatically detect your virtual environment and use it for running notebooks

🎉

You're Ready!

Your environment is set up with all course packages. Cursor will handle the rest!

🆕 Starting Your Own Project

When you want to create your own Python project from scratch:

$ uv init my-bio-project --python 3.12
$ cd my-bio-project
$ uv add jupyter numpy pandas matplotlib
$ cursor .

UV creates the project structure with Python 3.12, sets up the environment, and manages packages. Then open in Cursor to start coding!

⚠️ Important Notes

Cursor automatically detects your virtual environment - no manual activation needed!

The .venv folder can be large (100s of MB) - don't commit it to Git

Each project gets its own pyproject.toml file that tracks all dependencies

Use uv add package-name to install new packages (not pip!)

📋 Quick Reference

uv --versionCheck UV is installed
uv init project --python 3.12Create new project
uv add packageAdd a package to project
uv syncInstall all project dependencies
cursor .Open project in Cursor

That's it! UV manages your environment, and Cursor automatically uses it when you open notebooks.