Uv: Running a script with dependencies
Enthusiasm for uv’s script mode
- Many commenters call
uv run --scripta “killer feature” that revived their use of Python for one-off or small tools, especially in git hooks and ad‑hoc scripts. - Inline dependency blocks (PEP 723) are praised for letting a single
.pyfile be self-contained: just ship the script, installuv, and run. - The shebang pattern
#!/usr/bin/env -S uv run --scriptis widely used to make Python scripts feel like regular executables.
Shebang env -S portability discussion
- Long subthread explaining why
-Sis needed: historically, different Unix variants handled argument splitting in shebangs inconsistently. - Modern BSDs, macOS, and recent coreutils support
env -Sfor portable multi-arg shebangs; some systems (OpenBSD, NetBSD, Solaris, BusyBox) still don’t. - Consensus: using
env -Sis the safest cross-platform choice, even if it’s a no-op on some systems.
PEP 723, design trade-offs, and “magic comments”
- Strong support for PEP 723 as a standard way to embed dependencies; several tools (uv, pipx, hatch, marimo, Jupyter kernels) now use it.
- Debate over using “magic comments” vs real syntax:
- Pro: tools don’t need a full Python parser; Python core stays packaging-agnostic; works across language changes.
- Con: feels non-obvious as “syntax,” and people wish imports could carry version info directly.
- Multiple replies explain why inferring deps from imports is fragile: import names don’t map cleanly to distribution names or versions.
Tooling comparisons and ecosystem impact
- Some say uv halted plans to migrate scripts to Go, though Go binaries remain preferable for zero-runtime, airgapped use.
- Conda is criticized as heavy; uv plus wheels is seen as enough for most, though some note conda is still useful for complex C/C++/GPU stacks.
- uv is described as “pip + venv + pyenv + pipx” in one fast, coherent tool, leading some to wish it were the default Python toolchain.
Limitations, gotchas, and editor integration
- Single-file only: multi-module projects still need
pyproject.toml. - uv’s cache grows indefinitely; there’s
uv cache clean, but no GC yet. Hard links reduce disk cost, but don’t help across filesystems. - Offline/infra scripts must pre-warm caches or avoid this pattern; relying on runtime downloads can fail when the network is down.
- LSP/IDE integration is awkward: editors often don’t see uv’s transient venvs without manual configuration or helper scripts/extensions.
- Specific pain points: PyTorch wheel variants,
uv runproject discovery from outside the project dir, and SCA tools missing inline deps.