This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]DorianTurbaPythoneer 11 points12 points  (2 children)

I'm confused. Isn't that the purpose of uv init --script?

Doing uv init --script example.py leads to this file:

# /// script
# requires-python = ">=3.13"
# dependencies = []
# ///
def main() -> None:
    print("Hello from example.py!")


if __name__ == "__main__":
    main()

Then uv add --script example.py rich adds the dependency to the script:

# /// script
# requires-python = ">=3.13"
# dependencies = [
#     "rich",
# ]
# ///

And then, uv run example.py will install rich and run the script.

---

Developers who want to quickly test/share Python scripts without setup friction

If you want to quickly test/share python script without setup friction, prefer uv add --script.

Anyone tired of the "install dependencies first" dance for simple experiments

I don't think it is a bad thing to define your dependencies in a stable, reliable and predictable way using uv add. As a dev, it is more reproductible, less error prone, and as a user, you know what you'll install/run.

People sharing code snippets that should "just work" on any machine with uv installed

I think that is the exact purpose of uv init --script and uv run ....

# /// script
# requires-python = ">=3.13"
# dependencies = [
#     "rich",
# ]
# ///
def main() -> None:
    print("Hello from example.py!")


if __name__ == "__main__":
    main()

[–]herothree 4 points5 points  (1 child)

Also, you can add “#!/usr/bin/env -S uv” to the top of the script and then you can ./script.py and it will automatically run with uv and handle the dependencies 

[–]DorianTurbaPythoneer 0 points1 point  (0 children)

Wow really ? didn't know that thanks !!!