Python with uv
Use the repository's existing Python and verification choices first; use uv for dependency and execution operations unless a documented repository command wraps them.
Choose the Mode
- Project: a
pyproject.toml, lockfile, shared package, or local imports define an environment.
- Standalone script: one file or stdin should run without creating a project.
- Use
--no-project only when the invocation must ignore the surrounding project and does not import its code.
Project Workflow
- Inspect
pyproject.toml, lockfiles, repository instructions, and existing tools.
- Initialize only genuinely new projects with
uv init. Add or remove dependencies with uv add and uv remove; use uv sync to reconcile declared state.
- Run Python and project tools through
uv run or the repository's documented wrapper.
- Preserve the established test, lint, type-check, coverage, and hook stack. For a new project with no stated requirements, add only tools that serve the requested quality bar.
- Update and inspect the lockfile when dependency inputs change.
- Run the narrow checks needed during iteration, then the repository's aggregate gate.
Standalone Script Workflow
- Use
uv run script.py when no extra dependency is needed.
- Use
uv run --with <dependency> script.py for disposable dependencies.
- Use
uv init --script and uv add --script when dependencies or Python requirements should travel with a reusable script.
- Put
--no-project before the script name when isolation is intentional.
- Read
references/scripts.md only for inline metadata, stdin, shebang, locking, alternate indexes, or platform-specific patterns.
Quality and Release
Use verification in this order: documented aggregate gate; the repository's configured hook runner; individual uv run ... commands when no aggregate gate exists or when narrowing a failure. Do not introduce or switch tools solely to run checks. Read references/quality.md for fallback patterns.
For release preparation:
- Run the repository gate.
- Build with
uv build --no-sources so local source overrides cannot leak.
- Inspect wheel and sdist contents.
- Install and test the built wheel in a fresh uv-managed invocation.
- Record the artifact names, checks, and lockfile state.
Publishing is a separate external write. Do not run uv publish based only on a request to build, package, prepare, or release. Require authorization for the exact package/version, repository or index, and artifacts; use secret-safe credentials and verify the resulting release. Read references/packaging.md for the authorized publish path.
Invariants
- Do not use
pip install to mutate a uv-managed project.
- Do not create
pyproject.toml merely to run a one-file script.
- Do not use
--no-project when local project imports are required.
- Treat
--with as ephemeral; encode dependencies when reuse or reproducibility matters.
- Test the built or published artifact, not only the source checkout.
1---2name: manage-python-with-uv3description: Manage Python projects and standalone scripts with uv, including setup, dependencies, execution, quality gates, locking, builds, and release preparation. Use for `uv run`, `uv add`, inline script metadata, packaging, or an explicitly requested publish workflow.4---5
6# Python with uv
7
8Use the repository's existing Python and verification choices first; use uv for dependency and execution operations unless a documented repository command wraps them.
9
10## Choose the Mode
11
12- **Project:** a `pyproject.toml`, lockfile, shared package, or local imports define an environment.
13- **Standalone script:** one file or stdin should run without creating a project.
14- Use `--no-project` only when the invocation must ignore the surrounding project and does not import its code.
15
16## Project Workflow
17
181. Inspect `pyproject.toml`, lockfiles, repository instructions, and existing tools.
192. Initialize only genuinely new projects with `uv init`. Add or remove dependencies with `uv add` and `uv remove`; use `uv sync` to reconcile declared state.
203. Run Python and project tools through `uv run` or the repository's documented wrapper.
214. Preserve the established test, lint, type-check, coverage, and hook stack. For a new project with no stated requirements, add only tools that serve the requested quality bar.
225. Update and inspect the lockfile when dependency inputs change.
236. Run the narrow checks needed during iteration, then the repository's aggregate gate.
24
25## Standalone Script Workflow
26
271. Use `uv run script.py` when no extra dependency is needed.
282. Use `uv run --with <dependency> script.py` for disposable dependencies.
293. Use `uv init --script` and `uv add --script` when dependencies or Python requirements should travel with a reusable script.
304. Put `--no-project` before the script name when isolation is intentional.
315. Read `references/scripts.md` only for inline metadata, stdin, shebang, locking, alternate indexes, or platform-specific patterns.
32
33## Quality and Release
34
35Use verification in this order: documented aggregate gate; the repository's configured hook runner; individual `uv run ...` commands when no aggregate gate exists or when narrowing a failure. Do not introduce or switch tools solely to run checks. Read `references/quality.md` for fallback patterns.
36
37For release preparation:
38
391. Run the repository gate.
402. Build with `uv build --no-sources` so local source overrides cannot leak.
413. Inspect wheel and sdist contents.
424. Install and test the built wheel in a fresh uv-managed invocation.
435. Record the artifact names, checks, and lockfile state.
44
45Publishing is a separate external write. Do not run `uv publish` based only on a request to build, package, prepare, or release. Require authorization for the exact package/version, repository or index, and artifacts; use secret-safe credentials and verify the resulting release. Read `references/packaging.md` for the authorized publish path.
46
47## Invariants
48
49- Do not use `pip install` to mutate a uv-managed project.
50- Do not create `pyproject.toml` merely to run a one-file script.
51- Do not use `--no-project` when local project imports are required.
52- Treat `--with` as ephemeral; encode dependencies when reuse or reproducibility matters.
53- Test the built or published artifact, not only the source checkout.