3 ms·
These days, I am extremely happy to be using uv with single scripts. Put this at the start of your script (which has to end in .py): (EDIT: Sorry, HN doesn't l
by stavros 2y ago
These days, I am extremely happy to be using uv with single scripts. Put this at the start of your script (which has to end in .py):
(EDIT: Sorry, HN doesn't like code, see the start of https://github.com/skorokithakis/calumny/blob/master/calumny.py https://github.com/skorokithakis/calumny/blob/master/calumny... for an example)
The script will run with uv and automatically create a venv and install all dependencies in it. It's fantastic.
The other alternative, if you want to be extra sure, is to create a pex. It'll even bundle the Python interpreter in the executable (or download it if it's not available on the target machine), and will run anywhere with no other dependency (maybe libc? I forget).
- CamperBob2 2y agoThat looks pretty awesome. What are the drawbacks to using uv? Does it get along with existing pip and conda installations?
- stavros 2y agoI don't use conda, so I don't know, but it works fine with pip, it just makes its own virtualenv somewhere. It doesn't touch anything else.
- shlomo_z 2y agoIt is too fast and doesn't give you time to think. :) Just Kidding! It's amazing. It gets along with existing installations very well.
- sunshowers 2y agoOne minor drawback I've noticed is that direnv doesn't come with uv support out of the box: https://github.com/direnv/direnv/issues/1250 https://github.com/direnv/direnv/issues/1250
- infamia 2y agoI'm in the middle of replacing all of my usage of direnv with mise [1]. It does everything direnv/asdf can (for my use cases) plus a lot more. Mise can install a lot of dev software/frameworks [2], run tasks, file watcher all in a compatible manner for *nix OSes. mise is ridiculously good and even minimizes my need for docker locally. [1] https://mise.jdx.dev/ https://mise.jdx.dev/ [2] https://mise.jdx.dev/registry.html https://mise.jdx.dev/registry.html
- d4rkp4ttern 2y agoPoetry -> UV migration is missing: If you already have a project using Poetry, with a large pyproject.toml with many extras and other settings, there currently isn’t a smooth way to port this to UV. Someone can enlighten me if I missed that.
- simonw 2y agoHN displays code just fine if you put four spaces in front of each line, like this: #!/usr/bin/env -S uv run # /// script # requires-python = ">=3.12" # dependencies = [ # "google-api-python-client", # "google-auth-httplib2", # "google-auth-oauthlib", # "selenium", # "webdriver_manager", # "pydantic", # ] # /// import argparse import datetime I built a tool to help me do that: https://observablehq.com/@simonw/wrap-text-at-specified-width https://observablehq.com/@simonw/wrap-text-at-specified-widt... Edit: Huh, two spaces works too: #!/usr/bin/env -S uv run # /// script # requires-python = ">=3.12"
- Jtsummers 2y agoTwo spaces, Reddit is the four spaces one. Two spaces Four spaces
- cozzyd 2y agowhich is the right choice as :set ts=2 is obviously correct
- stavros 2y agoThanks Simon, I guess it was a brain fart, because I'm sure I've used this formatting a lot in the past.
- Flimm 2y agoHere are the official formatting guidelines: https://news.ycombinator.com/formatdoc https://news.ycombinator.com/formatdoc
- zahlman 2y ago>Put this at the start of your script (which has to end in .py): Yes, that format is specified by PEP 723 "Inline script metadata" (https://peps.python.org/pep-0723/ https://peps.python.org/pep-0723/). The cause was originally championed by Paul Moore, one of the core developers of Pip, who authored the competing PEP 722 (see also https://discuss.python.org/t/_/29905 https://discuss.python.org/t/_/29905) and was advocating for the general idea for long before that. It's also supported by Pipx, via the `run` subcommand. There's at least one pull request to put it directly into Pip, but Moore doesn't seem to think it belongs there.
- stavros 2y agoYep! It's just that uv installs everything fast enough to run scripts comfortably.
- ethbr1 2y agoFwiw, at the bottom the author recommends uv in almost all situations. (Except for non-Python dependencies, in which case Pixi)
- nextaccountic 2y agoWhy does it has to end with .py?
- stavros 2y agoOtherwise uv doesn't recognize it and goes into an infinite loop.
- karl42 2y agoIt doesn't have to. But if it does not end in .py, you have to add the --script (or -s for short) flag to tell it to interpret the file as a python script.
- stavros 2y agoOh, they added that? Very nice, I'll update my instructions and scripts, thanks!
- d4rkp4ttern 2y agoYou can go a step further and have scripts runnable from anywhere without cloning any repo or setting up any venv, directly from PyPi: when you package your lib called `mylib`, define cli scripts under [project.scripts] in your pyproject.toml, [project.scripts] mytool = "path.to.script:main" and publish to PyPi. Then anyone can directly run your script via uvx --from mylib mytool As an example, for Langroid (an open-source agent-oriented LLM lib), I have a separate repo of example scripts `langroid-examples` where I've set up some specific scripts to be runnable this way: https://github.com/langroid/langroid-examples?tab=readme-ov-file#run-scripts-with-no-repo-cloning-no-venv-setup https://github.com/langroid/langroid-examples?tab=readme-ov-... E.g. to chat with an LLM uvx --from langroid-examples chat --model ollama/qwen2.5-coder:32b or chat with LLM + web-search + RAG uvx --from langroid-examples chatsearch --model groq/llama-3.3-70b-versatile
- stavros 2y agoHm, I think you can just run something with 'uvx <name>' and it'll download and run it, am I misremembering? Maybe it's only when the tool and the lib have the same name, but I think I remember being able to just run 'uvx parachute'.
- d4rkp4ttern 2y agoYou're right, that's only when the tool and lib have the same name. In my case, I have several example scripts that I wanted to make runnable via a single examples lib
- stavros 2y agoThat's a really useful tip, thank you!