Lazy self-installing Python scripts with uv (treyhunner.com)
from norambna@programming.dev to python@programming.dev on 10 Dec 2024 09:36
https://programming.dev/post/22677736

via mastodon.social/@treyhunner/113624679999224789

#python

threaded - newest

danielquinn@lemmy.ca on 10 Dec 2024 12:10 next collapse

This looks like a reimplementation of pipx.

[deleted] on 10 Dec 2024 12:55 next collapse
.
Voroxpete@sh.itjust.works on 10 Dec 2024 13:22 next collapse

Correct me if I’m wrong, but I don’t think pipx can allow you to just put a shebang at the top of a script that automatically installs all the required dependencies the first time you run it?

What I really like about this, unless I’m missing something, is that it basically lets you create Python scripts that run in exactly the same way as shell scripts. I work with a lot of people who have pretty good basic Linux knowledge, but are completely at a loss when it comes to python specific stuff. Being able to send them a script that they can just +x and run sounds like a huge hassle saver.

danielquinn@lemmy.ca on 10 Dec 2024 14:25 collapse

Nope, pipx definitely can’t do that, but the idea that running your yourscript.py --help will automatically trigger the downloading of dependencies and installing them somewhere isn’t really appealing. I’m sure I’m not the only person who’s got uv configured to install the virtualenv in the local .venv folder rather than buried into my home dir, so this would come with the added surprise that every time I invoke the script, I’d get a new set of dependencies installed wherever I happen to be.

I mean, it’s neat that you can do this, but as a user I wouldn’t appreciate the surprise behaviour. pipx isn’t perfect, but at least it lets you manage things like updates.

eager_eagle@lemmy.world on 11 Dec 2024 00:53 collapse

I’m sure I’m not the only person who’s got uv configured to install the virtualenv in the local .venv folder

That’s the default for projects. Script is a different run mode.

every time I invoke the script, I’d get a new set of dependencies installed wherever I happen to be.

Does that happen though? uv uses the cache location for these script dependencies, not your usual venv one. Where you call it from is not a factor.

cd_slash_rmrf@programming.dev on 10 Dec 2024 23:18 collapse

uv actually does have a reimplementation of pipx, via uv tool or uvx: docs.astral.sh/uv/concepts/tools/#tools

the concept in the OP is different; it’s an implementation of pep722 peps.python.org/pep-0722/

RecallMadness@lemmy.nz on 11 Dec 2024 04:08 next collapse

Or, you could package it as a Pex.

  • it would just need a compatible Python version
  • no additionalional packaging tools need to be installed by your users
  • all of the dependencies packaged together once. as they were built and tested against.
  • doesn’t need installing. Doesn’t install anything. Just run it.
  • doesn’t depend on pypi being accessible.
sith@lemmy.zip on 11 Dec 2024 12:16 next collapse

uv is quite awesome.

CJJackson@programming.dev on 31 Dec 2024 12:20 collapse

I’ve been working on a SquashFS based deployment system in Python, the issue I came across was the fact that the permission system relied on tomllib, which is only available on Python 3.11 only the latest LTS Linux distros have that version or above.

But uv and that shebang trick, has helped me get round the limitations with ease and elegant.

I’d say hat off to that, thank you for sharing that! =)