Self-contained Python scripts with uv
Use cases and appeal
- Many see uv-based scripts as a lightweight alternative to Docker or full packaging, especially for internal tools and small utilities.
- uv’s speed and automatic Python/version handling are praised; it solves “which Python, which venv, which requirements” issues on heterogeneous machines.
- Common suggested use: repo-local dev/utility scripts, not necessarily end-user application distribution.
Windows and cross-platform behavior
- Shebang-based usage is tricky on Windows: native shebang isn’t supported, though workarounds include:
- Using the
pylauncher (from the CPython installer) which honors shebangs. - Re-associating
.py(or custom extensions) withuv run. - Running via WSL.
- Using the
- Some report success using
uv init/add/runon Windows, but flags like--devcurrently don’t work with--script.
Licensing, trust, and long-term viability
- Comparisons to Anaconda’s licensing change prompt questions about uv’s safety as a long-term dependency tool.
- Commenters note uv’s permissive licensing and that existing open-source code can’t be back-licensed, but also stress:
- Open source doesn’t guarantee maintenance.
- uv being written in Rust means relying on a Rust community that cares about Python.
Inline metadata vs comments / PEP 723
- Debate over using comment blocks for dependency metadata:
- Critics dislike executable behavior hinging on comments; prefer real Python data structures or JSON.
- Others argue for a declarative, restricted format; executing Python to discover dependencies would break portability and bootstrapping.
- Several highlight this is standardized via PEP 723 (inline script metadata), not uv-specific, and is deliberately tool-agnostic.
Editor, LSP, and tooling integration
- Some struggle getting LSPs (e.g., pyright) to see dependencies from inline metadata.
- Workarounds involve wrapper scripts that export requirements and re-run editors via uv.
- Requests for native support in editors (e.g., VS Code Python extension) are linked.
“Self-contained” vs reality
- Multiple comments challenge the “self-contained” label:
- uv must be preinstalled; scripts also fetch packages from the network and cache them.
- Caches/venvs persist unless explicitly cleared, though uv deduplicates packages.
- Compared approaches:
- Nix/nix-shell shebangs (stronger system requirements but no separate Python install).
- Classic bundlers/compilers (PyInstaller, py2exe, Nuitka) that produce true standalone binaries.
- Simple venv-based bootstrap scripts for environments where Python is already present.