uv Getting Started -- Python Package and Environment Management Tool
In Python development, package management and environment isolation are problems every developer encounters. Whether it is pip being slow, virtualenv being cumbersome, or conda being bloated, uv makes developers look forward to a more efficient solution.
What is uv?
uv is a Python package manager and environment manager developed by Astral and written in Rust; its main goal is to provide performance 10-100 times faster than existing tools while keeping a simple and intuitive user experience.
uv can replace pip, virtualenv, pip-tools, pyenv and other tools, providing one-stop services for dependency management, virtual environment creation and Python version management.
Advantages of uv
- Extremely fast: because it is written in Rust, uv far outperforms pip and other package management tools, with dependency installation speeds improved by 10-100 times.
- Integrated functionality: combines dependency resolution, package installation, environment management and Python version management in one, so there is no need to install and learn multiple tools.
- Deterministic builds: uv generates a
uv.lockfile, ensuring that exactly the same dependency versions are installed in any environment and avoiding the "it works on my machine" problem. - Compatible with existing tools: uv can handle
requirements.txtandpyproject.tomland can seamlessly replace pip in existing workflows.
Installing uv
Install on macOS
Homebrew is recommended:
brew install uv
Or use the official installation script:
curl -LsSf https://astral.sh/uv/install.sh | sh
Install on Linux
curl -LsSf https://astral.sh/uv/install.sh | sh
Install on Windows
Using Winget:
winget install uv
Or use the official installation script (PowerShell):
irm https://astral.sh/uv/install.ps1 | iex
After installation, verify that it succeeded:
uv --version
Output similar to the following indicates a successful installation:
uv 0.8.14 (Homebrew 2025-08-28)
1. Managing Python versions
uv has built-in Python version management, so there is no need to install extra tools such as pyenv.
View available Python versions:
uv python list
The output is similar to the following:
cpython-3.14.0rc2-macos-aarch64-none
cpython-3.13.7-macos-aarch64-none
cpython-3.12.11-macos-aarch64-none
cpython-3.11.13-macos-aarch64-none
cpython-3.10.18-macos-aarch64-none
cpython-3.9.6-macos-aarch64-none /usr/bin/python3
pypy-3.11.13-macos-aarch64-none
Install a specific Python version:
# Install the latest Python 3.12
uv python install 3.12
# Install specific version
uv python install 3.11.6
# Install PyPy version
uv python install pypy3.10
Set the global default Python version:
uv python default 3.12
Pin the Python version for the current project (this creates a .python-version file):
uv python pin 3.12
After running this command, a .python-version file is generated in the current project; opening it shows the version number:
3.12
2. Managing virtual environments
Create a virtual environment:
# Create a virtual environment named .venv in the current directory (using system default Python)
uv venv
# Create a virtual environment using the specified Python version
uv venv --python 3.12
Activate a virtual environment:
# macOS / Linux
source .venv/bin/activate
# Windows(PowerShell)
.venv\Scripts\activate
Deactivate a virtual environment:
deactivate
In daily development you can use
uv runto run scripts directly, without manually activating a virtual environment.
3. Package management (pip-compatible mode)
uv provides a command interface fully compatible with pip, so it can directly replace pip commands in existing workflows:
Install a package:
# Install latest version
uv pip install requests
# Install specific version
uv pip install requests==2.31.0
# Install in bulk from requirements.txt
uv pip install -r requirements.txt
Upgrade a package:
uv pip install --upgrade requests
Uninstall a package:
uv pip uninstall requests
View installed packages:
uv pip list
Export the current environment's dependencies to requirements.txt:
uv pip freeze > requirements.txt
4. Project management (recommended approach)
uv supports a modern project management approach centered on pyproject.toml , which is the more recommended way to use it than pip mode, especially suitable for team collaboration and multi-environment deployment.
Initialize a project
uv init my_project
cd my_project
This creates the following basic project structure:
my_project/
├── pyproject.toml # 项目配置和依赖声明
├── .python-version # 固定 Python 版本
├── README.md
└── main.py
Adding and removing dependencies
In project mode, it is recommended to use uv add and uv remove to manage dependencies; they automatically update pyproject.toml and uv.lock:
# Add production dependencies
uv add requests
# Add dependencies of a specific version
uv add "requests>=2.31.0"
# Add development dependencies (only used in development environment, e.g., testing frameworks)
uv add --dev pytest ruff
# Remove dependencies
uv remove requests
Install all project dependencies (uv sync)
After cloning a project or updating pyproject.toml , run the following command to install all dependencies in one step:
uv sync
Notes on uv sync:Similar to
pip install -r requirements.txt, and it will automatically update based onpyproject.tomlanduv.lockinstalls all dependencies, ensuring the environment is exactly the same as other developers'. If installation is slow, you can configure a local mirror source inpyproject.tomlto set a domestic mirror source:
[tool.uv] index-url = "https://pypi.tuna.tsinghua.edu.cn/simple"
Generate/update the lock file
uv lock
This command resolves the dependencies in pyproject.toml and generates (or updates)uv.lock files.uv.lock which should be committed to version control to ensure all team members use exactly the same dependency versions.
5. Running scripts (uv run)
uv run is a very practical command in uv that canwithout manually activating a virtual environmentrun scripts or commands directly; uv automatically finds and uses the correct environment:
# Run Python script directly
uv run main.py
# Run tests in the project
uv run pytest
# Run any command (executed in the virtual environment context)
uv run python -c "import requests; print(requests.__version__)"
Use uv run Compared with manually activating an environment, the advantages are: you will not use the wrong Python version by mistake, and you will not forget to activate the environment and fail to find packages, which makes it especially suitable for CI/CD and script automation.
Migrating to uv
If you are using other tools, you can migrate to uv as follows:
Migrate from pip + virtualenv:
# Create and activate virtual environment
uv venv
source .venv/bin/activate
# Install existing dependencies
uv pip install -r requirements.txt
Migrate from pip-tools:
# Compile dependencies (alternative to pip-compile)
uv pip compile requirements.in -o requirements.txt
# Sync environment (alternative to pip-sync)
uv pip sync requirements.txt
Migrate from poetry or pdm:
# Directly use the existing pyproject.toml
uv sync