← Back to context

Comment by notatallshaw

10 days ago

> uv pip install --system torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118

uv has a feature to get the correct version of torch based on your available cuda (and some non-cuda) drivers (though I suggest using a venv not the system Python):

> uv pip install torch torchvision torchaudio --torch-backend=auto

More details: https://docs.astral.sh/uv/guides/integration/pytorch/#automa...

This also means you can safely mix torch requirements with non-torch requirements as it will only pull the torch related things from the torch index and everything else from PyPI.

I love uv and really feel like I only need to know "uv add" and "uv sync" to be effective using it with python. That's an incredible feat.

But, when I hear about these kinds of extras, it makes me even more excited. Getting cuda and torch to work together is something I have struggled countless times.

The team at Astral should be nominated for a Nobel Peace Prize.

  • > "uv add"

    One life-changing thing I've been using `uv` for:

    System python version is 3.12:

        $ python3 --version
        Python 3.12.3
    

    A script that requires a library we don't have, and won't work on our local python:

        $ cat test.py
        #!/usr/bin/env python3
    
        import sys
        from rich import print
    
        if sys.version_info < (3, 13):
            print("This script will not work on Python 3.12")
        else:
            print(f"Hello world, this is python {sys.version}")
    

    It fails:

        $ python3 test.py
        Traceback (most recent call last):
        File "/tmp/tmp/test.py", line 10, in <module>
            from rich import print
        ModuleNotFoundError: No module named 'rich'
    

    Tell `uv` what our requirements are

        $ uv add --script=test.py --python '3.13' rich
        Updated `test.py`
    

    `uv` updates the script:

        $ cat test.py
        #!/usr/bin/env python3
        # /// script
        # requires-python = ">=3.13"
        # dependencies = [
        #     "rich",
        # ]
        # ///
    
        import sys
        from rich import print
    
        if sys.version_info < (3, 13):
            print("This script will not work on Python 3.12")
        else:
            print(f"Hello world, this is python {sys.version}")
    

    `uv` runs the script, after installing packages and fetching Python 3.13

        $ uv run test.py
        Downloading cpython-3.13.5-linux-x86_64-gnu (download) (33.8MiB)
        Downloading cpython-3.13.5-linux-x86_64-gnu (download)
        Installed 4 packages in 7ms
        Hello world, this is python 3.13.5 (main, Jun 12 2025, 12:40:22) [Clang 20.1.4 ]
    

    And if we run it with Python 3.12, we can see that errors:

        $ uv run --python 3.12 test.py
        warning: The requested interpreter resolved to Python 3.12.3, which is incompatible with the script's Python requirement: `>=3.13`
        Installed 4 packages in 7ms
        This script will not work on Python 3.12
    

    Works for any Python you're likely to want:

        $ uv python list
        cpython-3.14.0b2-linux-x86_64-gnu                 <download available>
        cpython-3.14.0b2+freethreaded-linux-x86_64-gnu    <download available>
        cpython-3.13.5-linux-x86_64-gnu                   /home/dan/.local/share/uv/python/cpython-3.13.5-linux-x86_64-gnu/bin/python3.13
        cpython-3.13.5+freethreaded-linux-x86_64-gnu      <download available>
        cpython-3.12.11-linux-x86_64-gnu                  <download available>
        cpython-3.12.3-linux-x86_64-gnu                   /usr/bin/python3.12
        cpython-3.12.3-linux-x86_64-gnu                   /usr/bin/python3 -> python3.12
        cpython-3.11.13-linux-x86_64-gnu                  /home/dan/.local/share/uv/python/cpython-3.11.13-linux-x86_64-gnu/bin/python3.11
        cpython-3.10.18-linux-x86_64-gnu                  /home/dan/.local/share/uv/python/cpython-3.10.18-linux-x86_64-gnu/bin/python3.10
        cpython-3.9.23-linux-x86_64-gnu                   <download available>
        cpython-3.8.20-linux-x86_64-gnu                   <download available>
        pypy-3.11.11-linux-x86_64-gnu                     <download available>
        pypy-3.10.16-linux-x86_64-gnu                     <download available>
        pypy-3.9.19-linux-x86_64-gnu                      <download available>
        pypy-3.8.16-linux-x86_64-gnu                      <download available>
        graalpy-3.11.0-linux-x86_64-gnu                   <download available>
        graalpy-3.10.0-linux-x86_64-gnu                   <download available>
        graalpy-3.8.5-linux-x86_64-gnu                    <download available>

  • Agreed, making the virtual environment management and so much else disappear lets so much more focus go to python itself.

Of all the great things people say about UV, this is the one that sold me on it when I found this option in the docs. Such a nice feature.