| ▲ | mubou2 6 days ago |
| How do you handle packages? I want scripts to a be a single file with a shebang, not a repo with a requirements.txt that I need to run in a venv. To me, this is the biggest blocker to using Python for any non-trivial scripting (which is precisely the kind where I wouldn't want to use bash), but I'd like to know how others deal with it. C# scripts let you reference packages in a comment at the top of the file, for example: https://devblogs.microsoft.com/dotnet/announcing-dotnet-run-... |
|
| ▲ | chillaranand 5 days ago | parent | next [-] |
| 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.... |
| |
| ▲ | simonw 2 days ago | parent | next [-] | | This 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 2 days ago | parent [-] | | I wish env -S was more portable. It's a newer feature of the coreutils env implementation and isn't supported elsewhere afaik. | | |
| ▲ | networked 9 hours ago | parent | next [-] | | You 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 (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" "$@"
# ...
| |
| ▲ | collinfunk 2 days ago | parent | prev [-] | | FreeBSD 6.0 added 'env -S'. They have adopted a few different GNU inspired options recently, which I am happy about. |
|
| |
| ▲ | presbyterian 2 days ago | parent | prev [-] | | As 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 11 hours ago | parent [-] | | Sure but the sub question here was about packages. If you are installing packages, then starting with installing uv should be fine. |
|
|
|
| ▲ | milliams 2 days ago | parent | prev | next [-] |
| https://peps.python.org/pep-0723/, e.g.: # /// script
# requires-python = ">=3.11"
# dependencies = [
# "requests<3",
# "rich",
# ]
# ///
import requests
from rich.pretty import pprint
resp = requests.get("https://peps.python.org/api/peps.json")
data = resp.json()
pprint([(k, v["title"]) for k, v in data.items()][:10])
|
|
| ▲ | Rucadi 2 days ago | parent | prev | next [-] |
| Nix allows you to do this with any language and required dependency:
https://wiki.nixos.org/wiki/Nix-shell_shebang |
| |
| ▲ | HumanOstrich 11 hours ago | parent | next [-] | | Now you have to set up Nix first and deal with that nightmare of a learning curve. Just to auto-install some dependencies for a script. Might as well rewrite all your scripts in Rust too while you're layering on unholy amounts of complexity. | | |
| ▲ | Diti 10 hours ago | parent [-] | | It’s like Vim, you learn it once, and you keep using it forever once you’re used to it. I’m so thankful to see a flake.nix file in every single cool project on code forges. | | |
| ▲ | HumanOstrich 10 hours ago | parent [-] | | Yea that's a common theme of excuses for both Rust and Nix. Wrong though, because most anyone who can use a computer at all can learn the basics of Vim. Seeing that flake.nix badge of complexity lets me know a project will be a nightmare to set up and will break every other week. It's usually right next to the Cargo.toml badge with 400 dependencies underneath. | | |
| ▲ | kokada 9 hours ago | parent [-] | | Nix with Flakes never randomly break, I still have projects from 3 or 4 years ago that I can still run `nix build` and getting it running. Yes, if you try to update the `flake.lock` this may introduce breakages, but this is expected if you're pining `nixos-unstable` instead of a stable branch. |
|
|
| |
| ▲ | albertoCaroM 2 days ago | parent | prev [-] | | Whoa! This is a revelation. I already loved Nix and used nix-shell extensively, but this is the missing piece: fully reproducible Python scripts without compromise. | | |
| ▲ | luz666 21 hours ago | parent [-] | | Or install direnv and put your dependencies into a shell.nix, setup the bash hook according the manual, create the .envrc with the content "use nix". Then type "direnv allow". Then you can do e.g. use other persons python scripts without modifying their shebang. |
|
|
|
| ▲ | eternityforest 6 days ago | parent | prev | next [-] |
| What can you do with bash that isn't in the stdlib of python? Generally the only nontrivial scripting I ever do is associated with a larger project, so I often already have a pyproject.toml and a UV environment, and I just add the dependencies to the dev group. |
| |
| ▲ | mubou2 6 days ago | parent [-] | | Well, that's kind of what I mean. For scripts in a python project, you can freely use whatever packages you need. But for one-off scripts, if you need bs4 or something, you're screwed. Either your script now has external dependencies or it requires special tooling. It just feels strange that C# of all languages is now a better scripting tool than Python, at least out of the box. I did notice uv has exactly the feature I'm looking for, though it's obviously third-party: https://docs.astral.sh/uv/guides/scripts/#declaring-script-d... Is everyone just using uv now instead of pip, perhaps? Or is just another alongside pipenv, conda, poetry, etc.? (Python's not my main these days, so I'm out of the loop.) | | |
| ▲ | jollyllama 2 days ago | parent | next [-] | | I don't understand. To return to GP's point, what can you do in bash that you can't do in Python? Said in another way, what does bash offer that you would need to tackle with a dependency in Python? My understanding is that there is no such thing, and accordingly, you can still end up with something that is better than bash if you just use Python and call out to other tools with subprocess. | | |
| ▲ | dizhn 2 days ago | parent [-] | | There's bash. Then you need better loops/conditionals and more usable structures. That's when one should think of using a scripting language instead. I think the parent goes too far after that and what he's talking about is not something bash can do (well). That said, a lot of very complicated things are actually written in bash. Distrobox I think is for example. | | |
| ▲ | DonHopkins a day ago | parent [-] | | >That said, a lot of very complicated things are actually written in bash. Distrobox I think is for example. They're only complicated BECAUSE they're written in bash. If they were written in Python they would be much less complicated, more modular, able to use many existing industrial strength well tested python modules instead of rolling their own ad-hoc ones, and much easier to maintain. |
|
| |
| ▲ | eternityforest 5 days ago | parent | prev [-] | | UV is taking over really fast, it seems to be much more popular any other option. I suspect conda still has some market share too but I've never needed it. | | |
|
|
|
| ▲ | lgas 7 hours ago | parent | prev | next [-] |
| I don't have much need of this personally, but I was playing around with an example from earlier in the thread and ended up with this: #!/usr/bin/env -S uv run --with sh --script
from sh import ifconfig
print(ifconfig("en0"))
which is a pretty nice experience assuming you already have `uv` in the target environment. |
|
| ▲ | bigstrat2003 2 hours ago | parent | prev | next [-] |
| Honestly, I just use a requirements.txt file. It's not as convenient as having a single file, it's true, but that is far outweighed by how much easier Python is to use than bash. So I'm giving up a 3/10 convenience but gaining a 10/10 convenience, which I think is worth it. |
|
| ▲ | froh 11 hours ago | parent | prev | next [-] |
| Isn't pipx addressing exactly that? once the script is non-trivial, 'install' it using pipx, in editable mode when you work on the script and as normal pipx installed cli utility otherwise. the venv part is then completely under the hood. |
|
| ▲ | a-dub 2 days ago | parent | prev | next [-] |
| what can you do with bash that you cannot do with the python standard library and shelling out? |
|
| ▲ | kjkjadksj 2 days ago | parent | prev | next [-] |
| What is wrong with writing a short wrapper in bash to activate a conda env before running the script? Too unsexy? |
|
| ▲ | DonHopkins a day ago | parent | prev | next [-] |
| Isn't the fact that bash doesn't have a wonderful ecosystem of reusable modules a much more enormous insurmountable problem than the fact that you have to install python modules? You're really missing the forest for the trees. Not only does bash not have a module system like python, or a vast ecosystem of modules like python, but also that it's much too weak and brittle a language to implement most of those modules that Python has, and can't even call native code libraries directly. Even with just its standard built in "batteries included" libraries and no extension modules or native code modules, Python is still much more powerful than bash and easier to code and maintain. If you're complaining about having to install Python modules, you're usually already doing something that's impossible or incredibly difficult to do in bash anyway. Even something as simple and essential as fetching files via http or parsing json. Bash has to call out to other programs to do that, but you have to install those programs too, and while Python can certainly call out to curl or wget or jq, it doesn't have to, since it has all that and more built in. There really is no comparison, because bash loses along so many dimensions at once compared to Python. |
|
| ▲ | paulddraper 2 days ago | parent | prev [-] |
| > How do you handle packages? The same way you handle them with bash? Install them? What are we talking about here? |
| |
| ▲ | fridder a day ago | parent [-] | | I think it is the fact that python packaging has been problematic for some time. setuptools/easy_install, pip/pipx, poetry, conda, uv all promise they will be the thing that "fixes" it | | |
| ▲ | bigstrat2003 an hour ago | parent | next [-] | | Eh. Python packaging is just fine. Set up a venv, install with pip, done. I think that the difficulty of installing Python packages is wildly overblown on this site. | | | |
| ▲ | paulddraper a day ago | parent | prev [-] | | What does Bash use for its packaging? Use that. | | |
| ▲ | bccdee a day ago | parent [-] | | Ok tbh bash uses the system package manager to install command-line utilities, and using the system package manager for python packages HAS been tried, and the parallel use of system packaging and independent package managers is part of why python package distribution is such a mess. | | |
| ▲ | skydhash 10 hours ago | parent [-] | | Why using independent package managers alongside the system one? I think the introduction of non system packagers is what brought us the whole mess we are in. Most system packagers allows for custom repositories. | | |
| ▲ | bccdee 3 hours ago | parent [-] | | Because the system package repository doesn't package everything & isn't always up to date. And if you introduce other repos to fix this, then you have an ecosystem with multiple instances of the same package distributed at different versions in different repositories, and God help you if you need multiple versions of one package, or if you need a different version of Python entirely. Nix could do it, but not anything else. No—system python is for the system's scripts, and user projects should have their dependencies sandboxed in a virtual environment. That's the only model that really works. |
|
|
|
|
|