uv icon indicating copy to clipboard operation
uv copied to clipboard

Document how to use `uv run` with a shebang

Open PaarthShah opened this issue 1 year ago • 3 comments

Summary

Add documentation about how to use a shebang (#!/usr/bin/env -S uv run) to run a python script with uv run without actually inputting uv run directly in the shell.

Test Plan

Tested locally using uv 0.3.0 on Pop_OS! 22.04

PaarthShah avatar Aug 21 '24 09:08 PaarthShah

Related: https://github.com/astral-sh/ruff/issues/13021#issuecomment-2302585295

ngnpope avatar Aug 21 '24 17:08 ngnpope

Cool. I'm hesitant to merge this until we address https://github.com/astral-sh/uv/issues/6360

zanieb avatar Aug 22 '24 14:08 zanieb

This now needs a --script at the end of the shebang line, and probably a warning in the docs about how important it is to avoid https://github.com/astral-sh/uv/issues/6360

~~I also wonder whether it will work if the first argument to the script could be confused with a uv run option. Not sure if there's any way for shebang and env to put -- after the script. Maybe this would need a special uv command or behavior.~~

Update: I don't think my worry from the last paragraph is a real problem. I thought --script takes an argument, so --script prog.py is the same as --script=prog.py, but that is not the case, and uv help run says that "Arguments following the command (or script) are not interpreted as arguments to uv."

ilyagr avatar Dec 16 '24 08:12 ilyagr

I believe this is now in the docs: https://docs.astral.sh/uv/guides/scripts/#using-a-shebang-to-create-an-executable-file

charliermarsh avatar Nov 02 '25 20:11 charliermarsh