Sub topic 14
Running command-line tools with uvx
This chapter assumes you can use a terminal and know what a virtual environment is. It also assumes you have read the earlier chapters on uv run, project environments, and dependency groups. Here you will learn when a tool belongs in a project and when it should run on its own.
What uvx does
uvx is a shortcut for uv tool run. It runs a command-line tool from a Python package without adding that package to your project. uv builds an isolated environment for the tool, runs the command, and keeps the environment in its cache so later runs start quickly.
uvx ruff check .
uvx pycowsay "hello"The first command lints the current directory with Ruff. Nothing is added to pyproject.toml, uv.lock, or .venv. Ruff does not need to be installed first.
Three properties follow from this design:
- Isolation. The tool's dependencies do not mix with your project's dependencies.
- No project changes. You can run a tool in any directory, even one with no
pyproject.toml. - Caching. The environment is reused until the requirement changes or the cache is cleaned.
uvx, uv run, and uv tool install
Three commands can run a tool. They differ in where the tool lives and which packages it can import.
| Command | Where the tool lives | Can it import your project? | Typical use |
|---|---|---|---|
| `uvx ruff` | Temporary cached environment | No | One-off or occasional tools |
| `uv run pytest` | The project's `.venv` | Yes | Tools that need your code or its dependencies |
| `uv tool install ruff` | A persistent tool environment, with the command on your `PATH` | No | Tools you use daily across many projects |
The first question matters most. Formatters such as Ruff or Black only read source files, so they work well with uvx. pytest imports your package, and mypy needs your dependencies to resolve types. Run these through uv run from a dev group, as in the [dependency-groups] section of the weather-cli project.
Choosing a command and version
By default the command name must match the package name. When they differ, use --from to name the package. The HTTPie package, for example, installs a command called http:
uvx --from httpie http --versionYou can pin a version in two equivalent ways:
uvx [email protected] --version
uvx --from "ruff==0.6.9" ruff --versionThe @ form is shorter and accepts an exact version or latest. Use --from when you need a range such as "ruff>=0.6,<0.7".
To add helper packages to the tool's environment, use --with. This is how plugins are usually supplied:
uvx --with mkdocs-material mkdocs --versionYou can also choose the Python version for the tool's environment:
uvx --python 3.12 ruff --versionRunning from git, paths, and your own project
--from accepts more than index names. It takes any requirement that uv understands, including git URLs and local paths.
uvx --from git+https://github.com/httpie/cli httpie --version
uvx --from ./some-local-package some-commandThis also lets you try your own project's entry point without installing it. From the weather-cli project root:
uvx --from . weatheruv builds the project into an isolated environment and runs the weather console script. The run happens in a clean environment, so it fails if your pyproject.toml is missing a dependency that your project's .venv happened to supply. Use uvx --from . as a check on your packaging.
Cache behavior
The first uvx run for a tool resolves and installs it. After that, uv reuses the cached environment as long as the request is satisfied. This has a side effect: uvx ruff does not always give you the newest Ruff. It gives you a version that satisfies the request, which may be one you ran last month.
You have three ways to control this:
- Write
uvx ruff@latestto get the newest release. - Pass
--refreshto revalidate cached metadata. - Pin a version, such as
[email protected], so results are repeatable.
For scripts, CI jobs, and shared documentation, pin the version. An unpinned uvx command can behave differently on two machines.
Installing tools persistently
If you run a tool every day, uv tool install puts it in a persistent environment and links its executables into a directory on your PATH.
uv tool install ruff
uv tool list
uv tool upgrade ruff
uv tool uninstall ruffIf the command is not found after installation, the executable directory may not be on your PATH. Run uv tool update-shell to add it, then open a new shell. uv tool dir shows where tool environments are stored.
Installed tools do not update themselves. uv tool upgrade respects the version constraint you installed with. If you ran uv tool install "ruff==0.6.9", an upgrade will not move past that pin. Reinstall with a new constraint to change it.
Common errors
The following mistakes come up often:
- "Package not found" or a wrong command. The command name differs from the package name. Use
--from package command. - ModuleNotFoundError when running a tool that tests your code.
uvx pytestruns in an isolated environment, so it cannot import your project. Useuv run pytestand keeppytestin thedevgroup. - A different result on a colleague's machine. An unpinned
uvxrequest resolved to different versions. Pin the tool version. - Command not found after
uv tool install. The tool directory is not onPATH. Runuv tool update-shell. - Expecting
uvxto changepyproject.toml. It never does. Useuv add --devto record a tool as a project dependency.
Applying it to the weather-cli project
For weather-cli, a sensible split looks like this:
- Keep
pytestin thedevdependency group and run it withuv run pytest, because tests importweather_cli. - Run formatting and linting with a pinned
uvx [email protected] ..., because Ruff only reads files. - Use
uvx --from . weatherbefore a release to confirm the console script works from a clean environment.
This keeps pyproject.toml and uv.lock limited to what the project needs at runtime and during testing. Tools that stand apart from the code stay outside them.
Exercise
Your project's tests fail under uvx pytest with ModuleNotFoundError: No module named 'weather_cli', but they pass when you run uv run pytest. Explain why, and say where pytest should be declared.
Show answer
uvx runs pytest in a separate, isolated environment that does not contain your project or its dependencies, so the import fails. uv run uses the project's environment, where weather_cli is installed. Declare pytest in the dev dependency group (uv add --dev pytest) and run it with uv run pytest.
Exercise
The httpie package provides a command named http. Write a uvx command that runs http --version using HTTPie version 3.2.2.
Show answer
uvx --from "httpie==3.2.2" http --version. The @ form (uvx [email protected]) would try to run a command called httpie, so --from is needed when the command and package names differ.