uv tool, uvx & Scripts
Some Python packages are not libraries for your project but commands you want on your machine: ruff, black, httpie, pre-commit, twine, cookiecutter. Installing them into a project's environment mixes their dependencies with yours; installing them globally with pip is how system Pythons break. The answer, popularised by pipx, is one isolated environment per tool with only its commands exposed on PATH. uv does the same with uv tool and uvx.
Installing a tool
uv tool install pytest 2>&1
echo "---"
uv tool list
ls ~/.local/bin
~/.local/bin/pytest --version
echo "--- where it lives"
uv tool dir
ls "$(uv tool dir)"Resolved 5 packages in 54ms
Downloading pygments (1.2MiB)
Downloaded pygments
Prepared 5 packages in 49ms
Installed 5 packages in 72ms
+ iniconfig==2.3.1
+ packaging==26.3
+ pluggy==1.7.0
+ pygments==2.21.0
+ pytest==9.1.1
Installed 2 executables: py.test, pytest
warning: `/home/learner/.local/bin` is not on your PATH. To use installed tools, run `export PATH="/home/learner/.local/bin:$PATH"` or `uv tool update-shell`.
---
pytest v9.1.1
- py.test
- pytest
py.test
pytest
pytest 9.1.1
--- where it lives
/home/learner/.local/share/uv/tools
pytest
Each tool gets its own environment under ~/.local/share/uv/tools/<name>, and only the package's console scripts are linked into ~/.local/bin (uv warns when that folder isn't on PATH; uv tool update-shell fixes your shell profile). The tool's dependencies (pluggy, pygments…) are invisible to everything else.
A package without console scripts can't be a tool:
uv tool install rich 2>&1 | grep -E 'executables|error'No executables are provided by package `rich`; removing tool
error: Failed to install entrypoints for `rich`
Useful variations:
| Command | Effect |
|---|---|
uv tool install "pytest==9.1.1" |
a specific version |
uv tool install pytest --with pytest-cov |
add plugins to the tool's environment |
uv tool install ./greet |
install your own CLI from a local project |
uv tool upgrade --all |
upgrade every tool within its constraints |
uv tool uninstall pytest |
remove the environment and the links |
uvx: run without installing
uvx (short for uv tool run) runs a tool in a temporary, cached environment. Nothing is added to PATH, and the next run reuses the cache:
uvx pytest --version 2>&1
uvx pytest --version
echo "--- a specific version, or a command from another package"
uvx --from 'pytest==9.1.1' py.test --version
uvx --from rich python -c 'import rich; print("rich is importable here")' 2>&1 | tail -1Downloading pygments (1.2MiB)
Downloaded pygments
Installed 5 packages in 346ms
pytest 9.1.1
pytest 9.1.1
--- a specific version, or a command from another package
pytest 9.1.1
rich is importable here
uvx ruff check . and uvx twine check dist/* are now common in READMEs and CI scripts: they pin and fetch the tool on demand, with no setup step.
Your own CLI as a tool
Tools come from any source uv can install: PyPI, a Git URL, or a local folder. That's a nice way to dogfood a CLI you're writing:
uv init -q greet
uv tool install ./greet 2>&1 | tail -1
~/.local/bin/greet
uv tool listwarning: `/home/learner/.local/bin` is not on your PATH. To use installed tools, run `export PATH="/home/learner/.local/bin:$PATH"` or `uv tool update-shell`.
Hello from greet!
greet v0.1.0
- greet
Scripts with inline metadata (PEP 723)
For a single-file script, a whole project is overkill, but the script still has dependencies. PEP 723 lets a script declare them in a special comment block, and uv run builds a matching (cached) environment on the fly:
uv init -q --script report.py
uv add -q --script report.py "rich>=13"
cat > body.py <<'PY'
from rich.console import Console
console = Console(width=40, color_system=None)
console.rule("Build report")
console.print("3 wheels built, 0 failed")
PY
sed -i '/^def main/,$d' report.py && cat body.py >> report.py
sed -n '1,/^# \/\/\//p' report.py | sed '/^#$/,/^# default/d'
echo "--- uv run report.py"
uv run report.py 2>&1 | grep -v -E '^Installed'# /// script
# requires-python = ">=3.13"
# dependencies = [
# "rich>=13",
# ]
# ///
--- uv run report.py
Downloading pygments (1.2MiB)
Downloaded pygments
───────────── Build report ─────────────
3 wheels built, 0 failed
The block between # /// script and # /// is TOML: requires-python, dependencies, and optional [tool.uv] settings. Anyone with uv can run the file with uv run report.py, and on Unix you can even make it executable with the shebang #!/usr/bin/env -S uv run --script. uv lock --script report.py writes a report.py.lock if you want the script's environment pinned too.
Note: As with
uv addin a project, this container'sUV_DEFAULT_INDEXsetting makes uv record the local index in the script's[tool.uv]section; the session filters those lines out.
Test yourself
What's the difference between uv tool install pytest and uvx pytest?
- None
- tool install creates a persistent environment and puts pytest on PATH; uvx runs it from a cached temporary environment without installing it
- uvx installs globally with pip
- tool install adds pytest to the project
Why did uv tool install rich remove the tool again?
- rich is broken
- rich provides no console scripts, so there's nothing to expose as a tool
- rich is not in the index
- Tools must start with 'py'
Where does a PEP 723 script declare its dependencies?
- requirements.txt next to it
- A
# /// scriptcomment block of TOML at the top of the file - pyproject.toml
- A docstring