Modern Environment Tools: uv and pixi#
In Week 1 we settled on conda + pip as
the course default: a blank conda environment for isolation and Python-version
control, then pip install -r requirements.txt for fast package installs. We also
previewed two newer tools, uv and pixi. This chapter is the promised closer look, and
since HW 3 it is more than a look: the homework now runs with uv.
Both tools ship a run subcommand that executes a command inside the project’s environment,
creating the environment first if it has to. So they blur the line between an environment
manager and a lightweight task runner like doit.
Why the homework uses uv#
From HW 3 on, the homework and the case studies ship a pyproject.toml and a uv.lock, and
every pipeline starts with one command:
uv run doit
Four reasons, each of which you have already run into.
One command on a machine that has nothing. The first
uv rundownloads Python 3.12 if it is missing, creates.venv/, installs the locked packages, and then runs the command. That is exactly what a GitHub Actions runner needs. It starts empty every time, so the live monitor’s workflow builds its whole environment in two steps:- name: Set up uv uses: astral-sh/setup-uv@v7 with: enable-cache: true - name: Update the monitor run: uv run doit latest
The same versions everywhere.
uv.lockpins every package the project uses, the ones you named and everything they depend on, to an exact version and a checksum. Your laptop, the course’s grading runner, and GitHub’s runner all get the same environment. A hand-writtenrequirements.txtpins only what someone wrote down, and the rest floats: the same file can install a different environment next month.Fast enough to rebuild on every run.
uvresolves and installs in seconds, andsetup-uvcaches the downloads between runs. When the environment is rebuilt from scratch every morning, that speed is what makes rebuilding it practical.Standard files.
pyproject.tomlis the standard Python project manifest, the same file the packaging tools read. Nothing in it is specific touv, anduv exportstill writes arequirements.txtfor anyone who prefers thefinmconda environment. HW 3 ships one.
Same code, four toolchains#
The in-class examples repository has a
software_environments/
directory that makes the comparison concrete. The exact same script (analyze.py, a tiny
moving-average crossover demo depending on numpy, pandas, and matplotlib) ships in four
subdirectories. Only the tooling around installing and running it changes:
Directory |
Tool |
Manifest |
One-liner to run |
|---|---|---|---|
conda alone |
|
|
|
conda + pip |
|
|
|
|
|
|
|
|
|
|
Clone that repo and follow along—the manifests below are taken directly from it.
uv#
uv is a single, very fast tool, written in Rust, that
replaces pip, venv, pip-tools, and pyenv all at once. One manifest
(pyproject.toml), one lockfile (uv.lock), one command to run.
Installing uv#
uv is a standalone binary—it doesn’t even need an existing Python:
# macOS / Linux
curl -LsSf https://astral.sh/uv/install.sh | sh
# or, if you already have Python
pip install uv
The core idea: uv run bootstraps everything#
The headline feature is that a single command sets up the entire environment on demand.
In the 03_uv example, you run:
uv run analyze.py
On the first run, uv will: download Python 3.12 if it isn’t already available, create a
.venv/, resolve and install the dependencies, write a uv.lock file, and then run your
script. Subsequent runs are essentially instant because the environment already exists.
If you prefer an explicit two-step flow, it’s:
uv sync # create/update the environment from pyproject.toml + lock
uv run analyze.py # run inside it
Quick start: which command changes which file#
Three things matter. pyproject.toml lists what the project asks for (pandas>=2.2); you
edit it, or uv add does. uv.lock records what that resolved to, every package at an
exact version; only uv writes it. .venv/ holds the installed packages. Commit the first
two, and keep .venv/ out of git.
Command |
|
|
|
|---|---|---|---|
|
adds |
re-locked |
|
|
read |
created, or updated if it no longer matches |
untouched |
|
read |
|
untouched |
|
read |
re-locked if out of date |
made to match the lock exactly; extra packages removed |
|
read |
re-locked if out of date |
missing packages added, extras left alone; then |
|
read |
error if out of date |
as above |
So uv lock is the command that writes the lock, and it is conservative: it changes a
version only when pyproject.toml no longer allows it, or when you ask with --upgrade or
--upgrade-package. In a freshly cloned HW repo, uv run doit reads the committed
uv.lock, checks that it still matches pyproject.toml, downloads Python if needed, builds
.venv/ with exactly the locked versions, and runs doit. It does not choose versions; the
lock already did.
You run uv lock once, on whatever machine you have. The lock it writes covers macOS, Linux,
and Windows, and every Python version that requires-python allows. That is why a lock made
on a Mac works on GitHub’s Ubuntu runner.
The manifest: a standard pyproject.toml#
uv doesn’t invent its own config format—dependencies live in the standard [project]
table of a pyproject.toml:
[project]
name = "finm-env-uv"
version = "0.1.0"
description = "Moving-average crossover demo, managed by uv"
requires-python = ">=3.12"
dependencies = [
"numpy>=2.0",
"pandas>=2.2",
"matplotlib>=3.9",
]
The companion uv.lock file (generated automatically) pins the exact resolved versions
of every package—direct and transitive—so a collaborator who runs uv sync gets a
byte-for-byte identical environment. This is the reproducibility win over a hand-maintained
requirements.txt.
It still speaks requirements.txt#
You don’t have to adopt pyproject.toml to benefit from uv’s speed. uv has a
pip-compatible interface that reads the very same requirements.txt you already use:
uv venv # create a .venv
uv pip install -r requirements.txt # like pip install, but much faster
This is the gentlest on-ramp: drop uv in front of your existing pip workflow and keep
everything else the same.
Scripts that carry their own dependencies (PEP 723)#
Not every piece of code needs a project. PEP 723 lets a
single .py file list its own dependencies in a comment block at the top, and uv run
reads that block. Here is a complete script, ten_year.py, that pulls the 10-year Treasury
yield from FRED:
# /// script
# requires-python = ">=3.12"
# dependencies = [
# "pandas",
# ]
# ///
import pandas as pd
url = "https://fred.stlouisfed.org/graph/fredgraph.csv?id=DGS10"
rates = pd.read_csv(url, parse_dates=["observation_date"], na_values=".")
print(rates.dropna().tail())
uv run ten_year.py
uv builds an environment with pandas just for this script, caches it so later runs start
at once, and ignores the dependencies of any project the file happens to sit in. Anyone with
uv can run the file you send them, with nothing to set up first. You don’t have to type the
header yourself:
uv init --script ten_year.py --python 3.12 # new file with an empty header
uv add --script ten_year.py pandas # adds pandas to the header
uv lock --script ten_year.py # optional: pins it in ten_year.py.lock
The uv guide Running scripts covers the rest.
Handy uv commands#
Command |
Does |
|---|---|
|
Sync the environment (if needed) and run the script |
|
Create/update |
|
Add a dependency (updates |
|
Re-resolve and rewrite |
|
Fast, pip-compatible install into the env |
|
Install and manage a Python version |
What uv does that conda + pip don’t#
Most of the difference comes from the lockfile and from uv run checking it every time. The
last row is where conda + pip comes out ahead.
conda + pip |
uv |
|
|---|---|---|
Lockfile |
By hand: |
|
Platforms the lock covers |
The machine you exported from |
macOS, Linux, Windows, and every Python in |
Lock out of date |
Nothing checks |
|
Packages you no longer list |
Stay installed: |
|
Before running code |
Create, activate, install |
Nothing: |
Dependencies for a single script |
Not supported |
PEP 723 header, then |
Run a tool without installing it |
A separate tool ( |
|
Copies of each package |
pip installs a separate copy into every environment |
One shared cache, linked into each |
Installs Python |
conda does, pip doesn’t |
Yes |
Build and publish packages |
Separate tools ( |
|
Non-Python binaries (compilers, C libraries) |
conda does |
No: PyPI only |
Trade-offs#
Pro: Extremely fast; manages Python versions too; standards-based
pyproject.toml; deterministic lockfile.Con: PyPI-only. It does not install from conda channels, so the non-Python binaries that
condapackages (compilers, system libraries, some scientific stacks) may not be available. That’s exactly the gappixifills.
pixi#
pixi gives you a uv-style workflow—one manifest, one lockfile,
an automatic per-project environment—but it resolves packages from the conda
ecosystem (conda-forge) instead of PyPI. So you get conda’s non-Python binaries with
modern speed and ergonomics. (It can also pull from PyPI when you need to.)
Installing pixi#
# macOS / Linux
curl -fsSL https://pixi.sh/install.sh | bash
Note that, like uv, pixi is self-contained: you do not need a separate conda
install for it to resolve conda packages.
The manifest: pixi.toml, with built-in tasks#
Here is the 04_pixi manifest. Notice the [tasks] table at the bottom—this is where
the task-runner flavor comes in:
[project]
name = "finm-env-pixi"
version = "0.1.0"
description = "Moving-average crossover demo, managed by pixi"
channels = ["conda-forge"]
platforms = ["osx-arm64", "osx-64", "linux-64", "win-64"]
[dependencies]
python = "3.12.*"
numpy = ">=2.0"
pandas = ">=2.2"
matplotlib = ">=3.9"
[tasks]
start = "python analyze.py"
To run it:
pixi run start # runs the `start` task defined above
pixi run python analyze.py # or run anything inside the environment
pixi shell # activate an interactive shell in the environment
On the first pixi run, pixi resolves the environment, installs it under .pixi/, and
writes a pixi.lock for exact, cross-platform reproducibility (note the explicit
platforms list).
Handy pixi commands#
Command |
Does |
|---|---|
|
Run the |
|
Run any command inside the environment |
|
Activate an interactive shell |
|
Add a conda dependency (updates manifest + lock) |
|
Add a PyPI dependency |
When would you pick pixi over uv?#
Reach for pixi when you need packages that live in the conda world—non-Python binaries,
or scientific packages that are easier to get from conda-forge than from PyPI. It’s the
modern, lockfile-first successor to the plain conda workflow. If your project is
pure-Python and all your dependencies are on PyPI, uv is usually the simpler, faster pick.
A note on the task-runner connection#
You’ll have noticed that both uv run analyze.py and pixi run start run something inside
the project’s environment—which is conceptually close to what a task runner like doit
does. The difference in scope is worth keeping straight:
uv run/pixi runguarantee a command executes in the correct, reproducible environment, andpixi’s[tasks]can give those commands friendly names.A dedicated task runner like
doitadds dependency tracking: it knows which tasks depend on which files, skips work that’s already up to date, and chains multi-step pipelines together.
In practice they compose well, and this course composes them: doit defines the pipeline,
and uv run doit makes sure it runs in the locked environment, on your laptop and on a
runner alike.
Takeaway#
conda only |
conda + pip |
uv |
pixi |
|
|---|---|---|---|---|
Package source |
conda channels |
conda (Python) + PyPI |
PyPI |
conda channels (+ PyPI) |
Manages Python version |
✅ |
✅ |
✅ |
✅ |
Non-Python binaries |
✅ |
✅ (via conda) |
❌ |
✅ |
Lockfile |
manual |
manual |
|
|
Speed |
slower |
faster installs |
very fast |
very fast |
Run command |
|
|
|
|
Same code, four toolchains. conda is the long-standing data-science default and the only
one here that natively handles non-Python binaries on its own channels; conda + pip is
the pragmatic hybrid this course started with; uv and pixi are the fast, modern,
lockfile-first tools—uv from the PyPI world, pixi from the conda world. The homework
uses uv from HW 3 on. Whatever you reach for, your requirements.txt and environment.yml remain the portable interchange
formats that tie them all together.