uv:Environment Management Tool
Install uv
- Run in Windows PowerShell
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"- Install using pip
pip install uv- Verify installation Run in cmd
uv --versionVirtual 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 # WindowsManage 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 syncWorkflow
# 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 syncRun 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.pyEmbedded dependency declaration in scripts (recommended)
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