Most active commenters
  • lxgr(3)

←back to thread

Fun with uv and PEP 723

(www.cottongeeks.com)
618 points deepakjois | 16 comments | | HN request time: 0.73s | source | bottom
1. jkingsman ◴[] No.44369910[source]
uv has been fantastic to use for little side projects. Combining uv run with `uv tool run` AKA `uvx` means one can fetch, install within a VM, and execute Python scripts from Github super easily. No git clone, no venv creation + entry + pip install.

And uv is fast — I mean REALLY fast. Fast to the point of suspecting something went wrong and silently errored, when it fact it did just what I wanted but 10x faster than pip.

It (and especially its docs) are a little rough around the edges, but it's bold enough and good enough I'm willing to use it nonetheless.

replies(4): >>44370062 #>>44374562 #>>44375253 #>>44391468 #
2. lxgr ◴[] No.44370062[source]
Truly. uv somehow resolves and installs dependencies more quickly than pyenv manages to print its own --help output.
replies(2): >>44370440 #>>44371442 #
3. mikepurvis ◴[] No.44370440[source]
I know there are real reasons for slow Python startup time, with every new import having to examine swaths of filesystem paths to resolve itself, but it really is a noticeable breath of fresh air working with tools implemented in Go or Rust that have sub-ms startup.
replies(4): >>44370568 #>>44370762 #>>44370852 #>>44374512 #
4. lxgr ◴[] No.44370568{3}[source]
The Python startup latency thing makes sense, but I really don't understand why it would take `pyenv` a long time to print each line of its "usage" output (the one that appears when invoking it with `--help`) once it's already clearly in the code branch that does only that.

It feels like like it's doing heavy work between each line printed! I don't know any other cli tool doing that either.

replies(1): >>44371450 #
5. theshrike79 ◴[] No.44370762{3}[source]
The "slowness" and the utter insanity of trying to make a "works on my computer" Python program work on another computer pushed me to just rewrite all my Python stuff in Go.

About 95% of my Python utilities are now Go binaries cross-compiled to whatever env they're running in. The few remaining ones use (API) libraries that aren't available for Go or aren't mature enough for me to trust them yet.

6. Spivak ◴[] No.44370852{3}[source]
Not to derail the Python speed hate train but pyenv is written in bash.

It's a tool for installing different versions of Python, it would be weird for it to assume it already had one available.

replies(1): >>44371587 #
7. heavyset_go ◴[] No.44371442[source]
Last time I looked, pyenv contributors were considering implementing a compiled launcher for that reason.

But that ship has sailed for me and I'm a uv convert.

8. heavyset_go ◴[] No.44371450{4}[source]
There's a launcher wrapper shell script + Python startup time that contributes to pyenv's slow launch times.
9. lxgr ◴[] No.44371587{4}[source]
Oh, that might actually explain the slow line printing speed. Thank you, solves a long standing low stakes mystery for me :)
10. mr_mitm ◴[] No.44374512{3}[source]
You don't have to import everything just to print the help. I try to avoid top-level imports until after the CLI arguments have been parsed, so the only import until then is `argparse` or `click`. This way, startup appears to be instant even in Python.

Example:

    if __name__ == "__main__":
        from myapp.cli import parse_args

        args = parse_args()

        # The program will exit here if `-h` is given

        # Now do the heavy imports

        from myapp.lib import run_app

        run_app(args)
replies(1): >>44381127 #
11. TZVdosOWs3kZHus ◴[] No.44374562[source]
No more dependency problems with mkdocs I ran into before every other month:

  uvx --with mkdocs-material --with mkdocs-material-extensions --with mkdocs-nav-weight mkdocs serve -a localhost:1337
Funnily enough it also feels like it is starting faster.
replies(1): >>44376310 #
12. mmcnl ◴[] No.44375253[source]
I agree uv is amazing, but it's not a virtual machine, it's a virtual environment. It runs the scripts on top of your OS without any hardware virtualization. The virtual environment only isolates the Python dependencies.
13. winterqt ◴[] No.44376310[source]
Is there a reason you didn’t explicitly pull in mkdocs as a dependency in that invocation? I guess uv will expose it/let you run it anyways due to the fact that it’s required by everything else you did specify.
replies(1): >>44377548 #
14. tfitz237 ◴[] No.44377548{3}[source]
its a `uvx` call, so the tool being invoked is `mkdocs`, and all the other dependencies are additions on top of that
15. mikepurvis ◴[] No.44381127{4}[source]
Another pattern, though, is that a top level tool uses pkg_resources and entry_points to move its core functionality out to verb plugins— in that case the help is actually the worst case scenario because not only do we have to scan the filesystem looking for what plugins are available, they all have to be imported in order to ask each for its help strings.

An extreme version of this is the colcon build tool for ROS 2 workspaces:

https://github.com/colcon/colcon-core/blob/master/setup.cfg#...

Unsurprisingly, startup time for this is not great.

16. bjornasm ◴[] No.44391468[source]
>It (and especially its docs) are a little rough around the edges, but it's bold enough and good enough I'm willing to use it nonetheles

Thought I was the only one thinking this. Got to open an issue, I think it would be nice to have some more examples showcasing different use cases.