uv for data science practitioners
Python Project Management with uv:
Setting up a python environment was an adventure and used to take a good chunk of time. Package Conflict was a part of life for data science practitioners. UV is a ray of hope.
Here are two checklists that I am using to help me with this part of work.
Checklist 1: Setting Up a NEW Python Project with uv
Prerequisite: Ensure uv is installed globally on your system (pipx install uv)
Steps:
Create Your Project Folder:
mkdir your_new_project_name cd your_new_project_name
Initialize Your Virtual Environment:
uv venv
Activate Your Virtual Environment:
Windows Command Prompt
.venv\Scripts\activate
Linux / macOS / WSL
source .venv/bin/activate
(Verify
(.venv)appears in your prompt)Define Your Dependencies (
pyproject.toml):Create a
pyproject.tomlfile in your project root and add your project metadata and declared dependencies under the[project]table.Example
pyproject.tomlsnippet:pyproject.toml
[project] name = "your-new-project" version = "0.1.0" requires-python = ">=3.8" dependencies = [ "requests2.31.0", "fastapi0.111.0", "uvicorn[standard]==0.30.1", ]
Generate Your Lock File (
requirements.txt):This command reads
pyproject.tomland creates a preciserequirements.txtwith all exact versions and hashes:uv pip compile pyproject.toml --output-file requirements.txt
Install Dependencies into Your Environment:
This installs everything from your
requirements.txtlock file into your active virtual environment:uv sync
(Optional but Recommended) Configure Git:
- Add
.venv/and__pycache__/to your.gitignorefile to prevent committing generated files - Ensure
pyproject.tomlandrequirements.txtARE committed
- Add
Adding New Packages (The Easy Way):
Add packages directly with
uv:uv add pandas==2.0.3
This command will:
- Automatically add the package to your
pyproject.toml - Update your lock file (
requirements.txt) - Install the package in your active environment
If the package doesn't install automatically, run:
uv sync
- Automatically add the package to your
Removing Packages:
To remove a package:
uv remove pandas
This will automatically update both files and uninstall from your environment.
Checklist 2: Porting an EXISTING Project to a New Environment with uv
This checklist assumes you've received a project (e.g., cloned from Git) that already has pyproject.toml and a requirements.txt lock file.
Prerequisite: Ensure uv is installed globally on your system (pipx install uv).
Steps:
Get the Project Code:
git clone
Or download and extract the project files
Navigate into the Project Directory:
cd your_project_name
(Verify Files Present):
Confirm that
pyproject.tomlandrequirements.txt(the lock file) are present in the project root.Create a New Virtual Environment:
uv venv
Activate Your Virtual Environment:
Windows Command Prompt
.venv\Scripts\activate
Linux / macOS / WSL
source .venv/bin/activate
(Verify
(.venv)appears in your prompt)Install Dependencies from the Lock File:
uv sync
This command will read the existing
requirements.txtlock file and rapidly install all the exact, pinned dependencies into your new virtual environment.