News for hackers. Stuff that matters.
Re:Uvtrick by Terretta on Friday September 06, 2024 @02:08PM (#41468407)
This is running a given function in its own env, which seems at least in part thanks to uv now offering single script dependency management.

See "Single File Scripts" in the recent release blog here: https://astral.sh/blog/uv-unified-python-packaging#single-fi...

Incidentally also using "rich" in this example, uv natively does this now (first adding deps to main.py itself, then showing the revised main.py):

  $ uv add --script main.py "requests<3" "rich"
  $ cat main.py
  
  # /// script
  # requires-python = ">=3.12"
  # dependencies = [
  #     "requests<3",
  #     "rich",
  # ]
  # ///
  import requests
  from rich.pretty import pprint
  
  resp = requests.get("https://peps.python.org/api/peps.json")
  data = resp.json()
  pprint([(k, v["title"]) for k, v in data.items()][:10])
The `uvtrick` tool assumes that the Python script in question uses inline script metadata. More on this can be found here:

- https://docs.astral.sh/uv/guides/scripts/#declaring-script-d...

- https://packaging.python.org/en/latest/specifications/inline...

I'm pulling this all the way into a comment because if you haven't been paying attention to `uv`, this snippet here shows how "this changes everything".

There are no comments on this comment yet.