Live data from Hacker News

Uv: Running a script with dependencies

docs.astral.sh

91–100 of 174 posts

Re: Uv: Running a script with dependencies

#92
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…

My question is: if you already have to put the "import" there, why not have a PEP to specify the version in that same import statement, and let `uv` scan that dependency? It's something I never understood.

Re: Uv: Running a script with dependencies

#93

Earlier quoted context omitted.

This is cool, but honestly I wish it was builtin language syntax not a magic comment, magic comments are kind of ugly. Maybe some day… (I realise there are some architectural issues with making it built-in syntax-magic comments are easier for external tools to parse, whereas the Python core has very limited knowledge of packaging and dependencies… still, one of these days…)

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.

Re: Uv: Running a script with dependencies

#95
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…

My question is: if you already have to put the "import" there, why not have a PEP to specify the version in that same import statement, and let `uv` scan that dependency? It's something I never understood.

They don't want tools like uv to have to parse Python, which is complex and constantly changes.

See also https://peps.python.org/pep-0723/#why-not-infer-the-requirem...

Re: Uv: Running a script with dependencies

#97
post #82
post #24

Earlier quoted context omitted.

shebang mode is also incredibly useful and allows execution like ./script.sh #!/usr/bin/env -S uv run --script # /// script # dependencies = [ # "requests

'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

Re: Uv: Running a script with dependencies

#98
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

Re: Uv: Running a script with dependencies

#99
I've been doing something similiar utilizing guix shell[0] setting my shebang to e.g.:

#!/usr/bin/env -S guix shell python python-requests python-pandas -- python3

in scripts for including per-script dependencies. This is language agnostic as long as the interpreter and its dependencies are available as guix packages. I think there may be a similiar approach for utilizing nix shells that way as well.

[0]: https://guix.gnu.org/manual/en/html_node/Invoking-guix-shell...

Re: Uv: Running a script with dependencies

#100
I kinda always struggled to grok python's package management and module system. UV is finally making things more understandable and just as good as the nodejs packaging and module system that I'm more comfortable with. This new feature is kinda awesome. Even surpassing the DX of the node ecosystem.
Post reply on HN