uv¶
uv 是一款的用 Rust 编写的极速 Python 包管理器,旨在统一 pip, pip-tools, poetry, virtualenv 的功能。uv 由 Astral 公司开发,它的另一款著名产品是 Ruff,一款用 Rust 编写的极速 Python 代码检查工具。
Installation¶
You will see the output like these:
downloading uv 0.9.17 x86_64-unknown-linux-gnu
no checksums to verify
installing to /home/xxx/.local/bin
uv
uvx
everything's installed!
Validate the installaion:
1. Managing projects¶
Similar to
npmin Node.js andcargoin Rust.
Step 1:
Initialize the project:
This will add:
pyproject.toml: the description file..python-version: lock the version of Python in the project.main.py: an example script.README.md.
Step 2:
Add dependencies(to replace pip install ):
# for example:
uv add requests
# output:
# Using CPython 3.12.3 interpreter at: /usr/bin/python3.12
# Creating virtual environment at: .venv
# Resolved 6 packages in 1.80s
# Prepared 5 packages in 450ms
# Installed 5 packages in 2ms
# + certifi==2025.11.12
# + charset-normalizer==3.4.4
# + idna==3.11
# + requests==2.32.5
# + urllib3==2.6.2
uv will:
- Create a virtual environment, defaultly
.venv. - Download and install
requests. - Update the
pyproject.toml. - Generate or update the
uv.lock.
Step 3:
You can the code without manually activating the virtual environment:
Step 4:
When you pull the code from others and need to build the environment first, just run:
uv will automatically install all the dependencies according to pyproject.toml, uv.lock or requirements.txt
Note
uv sync is a dependency-management command that works like pip install -r requirements.txt, but faster, more powerful, and more reliable. It can install every third-party package (dependency) your project needs.
If uv sync is too slow, you can point it to a Chinese mirror by adding the Tsinghua PyPI index. In your pyproject.toml, under the [tool.uv] section, add:
2. Managing Python¶
To replace
pyenv.
Check the available versions of Python:
uv python list
# output:
# cpython-3.15.0a2-linux-x86_64-gnu <download available>
# cpython-3.15.0a2+freethreaded-linux-x86_64-gnu <download available>
# cpython-3.14.2-linux-x86_64-gnu <download available>
# cpython-3.14.2+freethreaded-linux-x86_64-gnu <download available>
# cpython-3.13.11-linux-x86_64-gnu <download available>
# cpython-3.13.11+freethreaded-linux-x86_64-gnu <download available>
# cpython-3.12.12-linux-x86_64-gnu <download available>
# cpython-3.12.3-linux-x86_64-gnu /usr/bin/python3.12
# cpython-3.12.3-linux-x86_64-gnu /usr/bin/python3 -> python3.12
# cpython-3.12.3-linux-x86_64-gnu /usr/bin/python -> python3
# cpython-3.11.14-linux-x86_64-gnu <download available>
# cpython-3.10.19-linux-x86_64-gnu <download available>
# cpython-3.9.25-linux-x86_64-gnu <download available>
# cpython-3.8.20-linux-x86_64-gnu <download available>
# pypy-3.11.13-linux-x86_64-gnu <download available>
# pypy-3.10.16-linux-x86_64-gnu <download available>
# pypy-3.9.19-linux-x86_64-gnu <download available>
# pypy-3.8.16-linux-x86_64-gnu <download available>
# graalpy-3.12.0-linux-x86_64-gnu <download available>
# graalpy-3.11.0-linux-x86_64-gnu <download available>
# graalpy-3.10.0-linux-x86_64-gnu <download available>
# graalpy-3.8.5-linux-x86_64-gnu <download available>
Install the specified version of Python:
Set the global default version of Python:
Set the version of Python in the project
3. Running scripts¶
To replace
pipx.
If you have a single script script.py that depends on pandas ,there is no need to create a project. You can simply add this at the beginning of the script:
# /// script
# requires-python = ">=3.11"
# dependencies = [
# "pandas",
# ]
# ///
import pandas as pd
print("Pandas is ready!")
Then run:
uv will create a temporary and isolated environment to run the code, and delete it automatically when finished. It is very convenient for quick tests and experiments.
4. Managing virtual environment¶
To replace
pip.
Create and activate a virtual environment:
# Create a virtual environment named `.venv`
uv venv
# macOS/Linux
source .venv/bin/activate
# Windows
.venv\Scripts\activate
Install packages:
Export the dependencies: