Sub topic 14
Running command-line tools with uvx
This chapter assumes you can use a terminal, know what a virtual environment is, and have read the earlier chapters on uv init, dependencies, and uv run. It covers running Python command-line tools that are not part of your project.
What uvx does
Some tools, such as ruff, black, or httpie, are programs you run but do not import. Installing them into every project environment clutters your dependencies. uvx runs a tool in a temporary, isolated environment instead. It is an alias for uv tool run.
uvx ruff check .
uvx --from httpie http --versionThe first command downloads ruff into uv's cache, builds an isolated environment, and runs it. Later runs reuse the cache, so they start quickly. Nothing is added to your project's pyproject.toml or uv.lock.
The second command uses --from because the package name (httpie) differs from the command it provides (http).
Pinning a tool version
By default uvx uses the latest release that fits your Python version. For repeatable results, name the version.
uvx [email protected] --version
uvx --from "ruff>=0.6,<0.7" ruff --versionUse @latest to skip the cached version and fetch the newest one.
Installing tools you use often
uv tool install puts the tool in its own persistent environment and places its command on your PATH. Use it for tools you run daily.
uv tool install ruff
uv tool list
uv tool upgrade ruff
uv tool uninstall ruffEach installed tool gets a separate environment, so tools cannot break each other through conflicting dependencies. If ruff is not found after installing, run uv tool update-shell and restart your shell.
uvx versus uv run
The two commands differ in which environment they use.
| Command | Environment | Typical use |
|---|---|---|
| `uv run pytest` | Your project environment | Tools that must import your code or its dependencies |
| `uvx ruff check .` | A separate, isolated environment | Tools that only read files |
Linters and formatters usually work well with uvx because they only read source files. pytest and type checkers like mypy need to import your code and its libraries, so they belong in a dependency group and should run with uv run.
Common errors
- Command not found. The package name and command name differ. Use
--from package command. - Test runner cannot import your package. You ran it with
uvx, so it cannot see your project. Add it withuv add --dev pytestand useuv run pytest. - Different results on different machines. You ran an unpinned tool. Pin it with
tool@version, or add it to a dependency group souv.lockrecords it.
Exercise
You want to run mypy against your project, and it must see the libraries listed in your pyproject.toml. Should you use uvx mypy or uv run mypy, and what should you do first?
Show answer
Use uv run mypy. First add it with uv add --dev mypy so it is installed in the project environment. uvx mypy would run in an isolated environment that cannot see your project's dependencies, so imports would fail to resolve.