Skip to content

uv:Environment Management Tool

Install uv

  1. Run in Windows PowerShell
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"
  1. Install using pip
pip install uv
  1. Verify installation Run in cmd
uv --version

Virtual Environment Management

# Create virtual environment named .venv in current directory
uv venv

# Create virtual environment with specific Python version
uv venv --python 3.12

# Activate virtual environment
source .venv/bin/activate # Mac/Linux
.venv\Scripts\activate # Windows

Manage Dependencies

# Install specific package
uv pip install <package-name>

# Install dependencies from requirements.txt file
uv pip install -r requirements.txt

# List installed packages
uv pip list

# Create requirements.txt file
uv pip freeze > requirements.txt

# Uninstall package
uv pip uninstall <package-name>

Restore Environment on New Machine

If there is no venv environment, please first execute uv venv, activate the environment, then execute

uv sync

Workflow

# Initialize project, generate pyproject.toml file
uv init
# Add dependency to pyproject.toml and install
uv add <package-name>
# Generate or update lock file uv.lock, lock exact versions of all dependencies
uv lock
# Sync and install all dependencies based on pyproject.toml and uv.lock files
uv sync

Run Scripts

Run scripts within project

No need to manually run source .venv/bin/activate or .venv\Scripts\activate to activate environment, uv run handles everything for you.

uv run <script.py>

Run single-file scripts (no project structure)

uv run --with <package-name> <script.py>

# Example
uv run --with "requests" --with "pandas" my_standalone_script.py

Add # /// script comment block at top of script. Just run uv run my_standalone_script.py, uv will automatically read dependency information from comments and configure environment. This method greatly facilitates script sharing and collaboration.

# /// script
# dependencies = [
#     "requests>=2.28",
#     "pandas",
# ]
# ///

import requests
import pandas as pd
# ... your code

Released under the [BY-NC-ND License](https://creativecommons.org/licenses/by-nc-nd/4.0/deed.en).