Live data from Hacker News

Uv: Running a script with dependencies

docs.astral.sh

101–110 of 174 posts

Re: Uv: Running a script with dependencies

#101

As a tangent, one issue I face is how to force cursor to use uv over pip. I have placed it in rules and also explicitly add a rules.yml file in every agent conversation. It still tries to push through pip 4 out of 10 times. Any recommendations on best practice to force cursor to use uv will make a significant dent in productivity

more of a bandaid than anything else, but maybe something like this might help:

alias pip='echo "do not use pip, use uv instead" && false'

You can put that in your bashrc or zshrc. There's a way to detect if it's a cursor shell to only apply the alias in this case, but I can't remember how off the top of my head!

Re: Uv: Running a script with dependencies

#102
As a non-Python person, I find “uv” to be a confusing name.

For a long time I assumed it’s a Python wrapper for libuv which is a much older and well-known project. But apparently it’s Python package manager #137 “this one finally works, really, believe me”

Re: Uv: Running a script with dependencies

#104
I love this feature of uv but getting linters/language servers to pick up the venv when editing the files is a bit of a pain. I currently have a script 'uv-edit' which I am using to run Neovim with the correct environment:

  #!/bin/bash
  SCRIPT="$1"; shift
  uv sync --quiet --script "$SCRIPT" && exec uv run --python "$(uv python find --script "$SCRIPT")" nvim "$SCRIPT" "$@"

Re: Uv: Running a script with dependencies

#105
post #10

Oh nice, I was already a happy user of the uv-specific shebang with in-script dependencies, but the `uv lock --script example.py` command to create a lock file that is specific to one script takes it to another level! Amazing how this feels so natural and yet only appeared after 20+ years of Python packaging.

What’s your use case for locking dependencies on a single script? One things that’s useful to my organization is that we can then proceed to scan the lockfile’s declared dependencies with, e.g., `trivy fs uv.lock` to make sure we’re not running code with known CVEs.

Just better visibility into the dependencies that come with the script (exactly for things like vulnerability scanning that you mention). It's also easier for reproducibility in someone else's environment when I can give them the exact list of dependencies instead of having them resolve it themselves using the inline declarations. Explicit is better than implicit :-)

Re: Uv: Running a script with dependencies

#106
post #4

The "declaring script dependencies" thing is incredibly useful: https://docs.astral.sh/uv/guides/scripts/#declaring-script-d... # /// script # dependencies = [ # "requests Save that as script.py and you can use "uv run script.py" to run it with the specified dependencies, magically installed into a temporary virtual environment without you having to think about them at all. It's an implementation of Python PEP 723: h…

This is cool! This gave me the questionable idea of doing the same sort of thing for Go: https://github.com/imjasonh/gos (Not necessarily endorsing, I was just curious to see how it would go, and it worked out okay!)

That's fun. I gave this a try myself. I took a different approach to the solution and used a bash script.

https://gist.github.com/JetSetIlly/97846331a8666e950fc33af9c...

Re: Uv: Running a script with dependencies

#107

Earlier quoted context omitted.

I thought that was a heart emoticon next to requests, for a second.

I mean, who doesn't love requests?

Me, because nearly every time I see it used, it’s for a trivial request or two that can easily be handled with stdlib.

If you need it, sure, it’s great, but let’s not encourage pulling in 3rd party libs unnecessarily.

Re: Uv: Running a script with dependencies

#108
post #93

Earlier quoted context omitted.

It IS built-in language syntax. It's defined in the PEP, that's built-in. It's syntax: "Any Python script may have top-level comment blocks that MUST start with the line # /// TYPE where TYPE determines how to process the content. That is: a single #, followed by a single space, followed by three forward slashes, followed by a single space, followed by the type of metadata. Block MUST end with the line # ///. That is…

Semantic whitespace was bad enough, now we have semantic comment blocks? I'm mostly joking, but normally when people say language syntax they mean something outside a comment block.

I am - while awesomely valuing uv - also with the part of the audience wondering/wishing this capability were a language feature.-

Re: Uv: Running a script with dependencies

#109
post #41

One gotcha I caught myself in with this technique is using it in a script that would remediate a situation where my home has lost internet and needed the router to be power cycled. When the internet is out, `uv` cannot download the dependencies specified in the script, and the script would fail. Thankfully I noticed this problem after writing it but before needing it to actually work, and refactored my setup to pre-i…

Here's a fun one (says he, in a panic): Will we get to a point where (through fault or, gasp, design) a given AI will generatively code a missing dependency, on the fly - perhaps as a "last ditch" effort?

(I can imagine languages having official LLMs which would more or less "compress/know" enough of the language to be an ...

... import of last resort, of sorts, by virtue of which an approximation to the missing code would be provided.-

Re: Uv: Running a script with dependencies

#110
post #82

Earlier quoted context omitted.

'env -s' it's not portable.

Should be portable as long as there are no quotes. It’s basically a nop on macOS since in macOS the shebang args are already split by spaces (even when quoted). Edit: further reading https://unix.stackexchange.com/a/605761/472781 > and https://unix.stackexchange.com/a/774145/472781 > and also note that on older BSDs it also used to be like that

Not under OpenBSD.
Post reply on HN