uv
An extremely fast Python package and project manager, written in Rust.
快速开始
安装
手动安装
curl -LsSf https://astral.sh/uv/install.sh | sh
# or
wget -qO- https://astral.sh/uv/install.sh | sh
pip(不建议,要用 uv 就抛弃 pip 了)
pip install uv
安装 python
uv python install
: Install Python versions.uv python list
: View available Python versions.uv python find
: 列出已安装的 python versionsuv python pin
: Pin the current project to use a specific Python version.uv python uninstall
: Uninstall a Python version.
管理项目
Creating and working on Python projects, i.e., with a pyproject.toml
.
- ==
uv add
: Add a dependency to the project.== uv remove
: Remove a dependency from the project.uv init
: Create a new Python project.- ==
uv sync
: Sync the project's dependencies with the environment.== uv lock
: Create a lockfile for the project's dependencies.uv tree
: View the dependency tree for the project.uv build
: Build the project into distribution archives.uv publish
: Publish the project to a package index.
安装工具
Running and installing tools published to Python package indexes, e.g., ruff
or black
.
uvx
/uv tool run
: Run a tool in a temporary environment.uv tool install
: Install a tool user-wide.uv tool uninstall
: Uninstall a tool.uv tool list
: List installed tools.uv tool update-shell
: Update the shell to include tool executables.
其他
Managing and inspecting uv's state, such as the cache, storage directories, or performing a self-update:
uv cache clean
: Remove cache entries.uv cache prune
: Remove outdated cache entries.uv cache dir
: Show the uv cache directory path.uv tool dir
: Show the uv tool directory path.uv python dir
: Show the uv installed Python versions path.uv self update
: Update uv to the latest version.
Python 安装与版本管理
$ uv python install 3.12
# multiple
$ uv python install 3.11 3.12
如果环境中已有 Python,uv 也会自动使用之,且不需要额外的配置
虚拟环境
uv supports creating virtual environments, e.g., to create a virtual environment at .venv
:
uv venv
A specific name or path can be specified,
uv venv <name>
这将会在当前目录新建一个 <name>
目录
A Python version can be requested, e.g., to create a virtual environment with Python 3.11:
uv venv --python 3.11
Note this requires the requested Python version to be available on the system. However, if unavailable, uv will download Python for you. See the Python version documentation for more details.
To exit a virtual environment, use the deactivate
command:
deactivate
包管理
安装
$ uv pip install flask
To install a package with optional dependencies enabled, e.g., Flask with the "dotenv" extra:
$ uv pip install "flask[dotenv]"
一次安装多个包
$ uv pip install flask ruff
指定版本
$ uv pip install 'ruff==0.3.0'
$ uv pip install 'ruff>=0.2.0'
从本地安装
$ uv pip install "ruff @ ./projects/ruff"
从 GitHub 安装
$ uv pip install "git+https://github.com/astral-sh/ruff"
To install a package from GitHub at a specific reference:
$ # Install a tag
$ uv pip install "git+https://github.com/astral-sh/ruff@v0.2.0"
$ # Install a commit
$ uv pip install "git+https://github.com/astral-sh/ruff@1fadefa67b26508cc59cf38e6130bde2243c929d"
$ # Install a branch
$ uv pip install "git+https://github.com/astral-sh/ruff@main"
Install from a requirements.txt
file:
$ uv pip install -r requirements.txt
See the uv pip compile
documentation for more information on requirements.txt
files.
Install from a pyproject.toml
file:
$ uv pip install -r pyproject.toml
Install from a pyproject.toml
file with optional dependencies enabled, e.g., the "foo" extra:
$ uv pip install -r pyproject.toml --extra foo
Install from a pyproject.toml
file with all optional dependencies enabled:
$ uv pip install -r pyproject.toml --all-extras
卸载包
$ uv pip uninstall <pkg-name>
To uninstall multiple packages, e.g., Flask and Ruff:
$ uv pip uninstall flask ruff
查看包信息
To list all of the packages in the environment:
$ uv pip list
# in a JSON format
$ uv pip list --format json
# in a requirements.txt format
$ uv pip freeze
To show information about an installed package, e.g., numpy
:
$ uv pip show numpy