top | item 44455204

(no title)

cssanchez | 8 months ago

I don't mean to be rude, but I don't get how this is any better. Feels too manual to type "uv -add dep script.py" instead, I feel the automation tool I'm waiting for will scan my script, auto-import all the deps I'm calling in the script while ignoring the ones that I forget to use, AND set up the env with all the deps AND run the code in the same one liner. To me, uv add X is no different than running env/pip install requirements.txt.

discuss

order

CraigJPerry|8 months ago

Compare the before vs after

Before (analogous to go mod init):

    python -m venv venv
    source venv/bin/activate
    python -m pip install -U pip
    pip install httpx
    pip freeze > requirements.txt
    nvim foo.py
    # find a way to share foo.py and requirements.txt
On another machine (still the before scenario, this time analogous to maybe go run):

    python -m venv venv
    source venv/bin/activate
    python -m pip install -U pip
    pip install -r requirements.txt
    python foo.py
In the after scenario:

    uv run foo.py
That's it. Comparable to

    ./my-go-binary

notatallshaw|8 months ago

What people like about this workflow is that you're not maintaining a separate venv or a separate requirement and it's declarative rather than imperative, this gives you two big advantages:

First, you can move that script to a different machine and do `uv run {script}`, no need to recreate a venv or provide install instructions (I believe uv will now even grab an appropriate version of Python if you don't have it?). This comes from PEP 723, and multiple tools support doing this, such as hatch.

Second, when you "add" a requirement instead of "install" a requirement it manages that with the knowledge of all requirements that were added before. For example, if I `pip install foo` and then `pip install bar` pip does not consider foo or it's dependencies as required when installing bar, so it's possible that you can break `foo` by installing completely incompatible dependencies. But when you "add foo" and then "add bar" from uv (and other tools that are declarative, like Poetry) your environment gets updated to take everything into account.

If managing Python dependencies is second nature to you then these might seem like extra concepts to keep in your head, but lots of people do find these useful because they find they can think less about Python dependencies.