Live data from Hacker News

Uv: Running a script with dependencies

docs.astral.sh

51–60 of 174 posts

Re: Uv: Running a script with dependencies

#51
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!)

Re: Uv: Running a script with dependencies

#52
post #31

Earlier quoted context omitted.

> he said his original goal was just to raise $5k to buy a computer. Privately, I was skeptical that the $5k computer had anything to do with Requests. Requests is a small pure-Python library; if you want to work on it, then any cheap laptop is more than sufficient. $5k is the price of a beefy server or top-end gaming rig. Kenneth Reitz has probably done more to enrich my life than most anyone else who builds things.…

What has he built that you like using that much? Honest question, not being snarky. I liked Requests way back when but prefer httpx or aiohttp. I liked piping for about a month when it first came out, but jumped ship pretty quickly. I'm not familiar with his other works. I also wouldn't begrudge the guy a laptop, but I do get what the author was saying. His original fundraiser felt off , like, if you want a nice lapt…

> I liked Requests way back when but prefer httpx or aiohttp.

Those two tools are modeled after `requests`, so Reitz still has an influence in your life even if you don't use his implementation directly.

Re: Uv: Running a script with dependencies

#53
post #52

Earlier quoted context omitted.

What has he built that you like using that much? Honest question, not being snarky. I liked Requests way back when but prefer httpx or aiohttp. I liked piping for about a month when it first came out, but jumped ship pretty quickly. I'm not familiar with his other works. I also wouldn't begrudge the guy a laptop, but I do get what the author was saying. His original fundraiser felt off , like, if you want a nice lapt…

> I liked Requests way back when but prefer httpx or aiohttp. Those two tools are modeled after `requests`, so Reitz still has an influence in your life even if you don't use his implementation directly.

They’re pretty close, sure, but Requests itself basically models a web browser. The newer ones model newer browsers (eg with HTTP/2 and async).

Re: Uv: Running a script with dependencies

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

But don't you have to only ever run it once to have the deps/venv for subsequent runs?

Re: Uv: Running a script with dependencies

#56
Heard about this! Finally did something with it and I'm happy with how this tiny gist turned out:

https://gist.github.com/pythoninthegrass/e5b0e23041fe3352666...

tl;dr

Installs 3 deps into the uv cache, then does the following:

1. httpx to call get request from github api

2. sh (library) to run ls command for current directory

3. python-decouple to read an .env file or env var

Re: Uv: Running a script with dependencies

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

But don't you have to only ever run it once to have the deps/venv for subsequent runs?

I don't know if uv garbage collects its cache, but it wouldn't surprise me. Otherwise disk usage would grow indefinitely.

Re: Uv: Running a script with dependencies

#59
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, 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…)

Re: Uv: Running a script with dependencies

#60
post #23

How many package managers can one language have? Its a simple language but setting it up is just incredibly bad. Maybe this is the one or should I wait for the next?

I had the same sentiment, but uv seems to have eliminated the competition. Installing uv using your OS package manager is enough as it can also download and install (isolated) Python interpreters as well.
Post reply on HN