Hacker News new | past | comments | ask | show | jobs | submit login

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... 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).




HN 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-widt...

Edit: Huh, two spaces works too:

  #!/usr/bin/env -S uv run
  # /// script
  # requires-python = ">=3.12"


Two spaces, Reddit is the four spaces one.

  Two spaces
    Four spaces


which is the right choice as :set ts=2 is obviously correct


Here are the official formatting guidelines: https://news.ycombinator.com/formatdoc


Thanks Simon, I guess it was a brain fart, because I'm sure I've used this formatting a lot in the past.


You 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-...

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


Hm, 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'.


You'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


That's a really useful tip, thank you!


>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/). 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) 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.


Yep! It's just that uv installs everything fast enough to run scripts comfortably.


Fwiw, at the bottom the author recommends uv in almost all situations. (Except for non-Python dependencies, in which case Pixi)


That looks pretty awesome. What are the drawbacks to using uv? Does it get along with existing pip and conda installations?


I 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.


One 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


I'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/

[2] https://mise.jdx.dev/registry.html


Poetry -> 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.


It is too fast and doesn't give you time to think. :)

Just Kidding! It's amazing. It gets along with existing installations very well.


Why does it has to end with .py?


It 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.


Oh, they added that? Very nice, I'll update my instructions and scripts, thanks!


Otherwise uv doesn't recognize it and goes into an infinite loop.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: