3 ms·
You can specify requirements at the top of file and uv can run the script after automatically installing the dependencies. https://avilpage.com/2025/04/learn-p
by chillaranand 10mo ago
You can specify requirements at the top of file and uv can run the script after automatically installing the dependencies.
https://avilpage.com/2025/04/learn-python-uv-in-100-seconds.html https://avilpage.com/2025/04/learn-python-uv-in-100-seconds....
- simonw 10mo agoThis works really well in my experience, but it does mean you need to have a working internet connection the first time you run the script. # /// script # dependencies = [ # "cowsay", # ] # /// import cowsay cowsay.cow("Hello World") Then: uv run cowscript.py It manages a disposable hidden virtual environment automatically, via a very fast symlink-based caching mechanism. You can also add a shebang line so you can execute it directly: #!/usr/bin/env -S uv run --script # # /// script # dependencies = ["cowsay"] # /// import cowsay cowsay.cow("Hello World") Then: chmod 755 cowscript ./cowscript
- emidln 10mo agoI wish env -S was more portable. It's a newer feature of the coreutils env implementation and isn't supported elsewhere afaik.
- collinfunk 10mo agoFreeBSD 6.0 added 'env -S'. They have adopted a few different GNU inspired options recently, which I am happy about.
- networked 10mo agoYou can use so-called "exec magic" instead of `env -S`. Here is an explanation with Python examples: https://dbohdan.com/scripts-with-dependencies#exec-magic https://dbohdan.com/scripts-with-dependencies#exec-magic (disclosure: my site). In short: #! /bin/sh "exec" "/usr/bin/env" "uv" "run" "--quiet" "--script" "$0" "$@" # /// script # dependencies = [ # "cowsay", # ] # /// import cowsay cowsay.cow("Hello, world!") On systems that can't run uv, like NetBSD and OpenBSD, switch to pipx: #! /bin/sh "exec" "/usr/bin/env" "pipx" "run" "$0" "$@" # ...
- vovavili 10mo agoIf not having a working internet connection is a concern, I would package your script into a shiv zipapp - https://shiv.readthedocs.io/en/latest/ https://shiv.readthedocs.io/en/latest/
- presbyterian 10mo agoAs someone who writes a lot of python, I love uv, but isn't on nearly every system like python is, which is one of the arguments for using python here in the first place
- ewidar 10mo agoSure but the sub question here was about packages. If you are installing packages, then starting with installing uv should be fine.
- vovavili 10mo agoThe catch is that there could be a version mismatch between the version of Python installed on the end user's computer and the version on which the script was developed. This problem can be solved with uv, and there aren't really Python-native ways available.